?AdarieDiscord
Sign in / Sign up

Python Libraries for Go (Baduk/Weiqi) in Research

by Peter de Blanc + ChatGPT Deep Research

Introduction

Researchers working with the game of Go often need tools to simulate games, parse game records, and leverage AI engines. Key requirements include:

  1. Board State Simulation: Given a sequence of moves, the library should accurately produce the resulting board state, handling captures and ko rules correctly.
  2. SGF Parsing: Ability to read and navigate SGF (Smart Game Format) files, including traversing game tree variations and accessing node comments/annotations.
  3. AI Integration (KataGo Policy): Launching a KataGo engine process and retrieving its neural network policy output for a given position (the AI’s move probabilities).

No single Python library on PyPI fully covers all three areas out-of-the-box, but several projects provide overlapping functionality. Below we evaluate the most relevant libraries – their features, ease of use, documentation, maintenance, and limitations – and recommend an optimal combination to fulfill all requirements.

Libraries Overview

Sente

Sente is a modern, general-purpose Go library written in C++ with Python bindings (installable via pip). It supports game simulation and SGF handling, designed as a Python 3 replacement for the older Gomill library (GitHub - atw1020/sente: Sente is an open source python library for the game of Go). With Sente, you can create a game, play moves (with rules enforcement), and print or inspect the board state at any point. It uses unicode symbols for stones and tracks ko and captures correctly as part of its rules implementation. Sente also includes an SGF module to load and save game records (GitHub - atw1020/sente: Sente is an open source python library for the game of Go) (GitHub - atw1020/sente: Sente is an open source python library for the game of Go), allowing traversal of game trees and access to properties like comments.

Strengths: Sente covers both board-state simulation and SGF parsing/editing in one library. It is optimized (C++ core) for performance (GitHub - atw1020/sente: Sente is an open source python library for the game of Go), and it provides extensive documentation and tutorials (via ReadTheDocs). It supports different rule sets (Chinese by default, with Japanese as an option) and multiple board sizes (GitHub - atw1020/sente: Sente is an open source python library for the game of Go). In practice, Sente makes it straightforward to apply a sequence of moves from an SGF: one can load a game record and play through moves programmatically, with captures and ko handled internally.

Limitations: Sente does not natively provide a GTP client or direct integration for external AI engines. It only implements the engine side of the Go Text Protocol, not the controller side (GTP Introduction — Sente 0.4.4 documentation). This means to use KataGo, you must manually launch KataGo (e.g. via Python’s subprocess) and communicate with it. Sente’s documentation explicitly notes that users wanting to host a GTP engine should use their own subprocess code (GTP Introduction — Sente 0.4.4 documentation). Thus, requirement #3 (KataGo policy output) is not a built-in feature – you would need to run KataGo separately and parse its output. Additionally, Sente’s support for Japanese rules is incomplete (no automatic dead stone removal for scoring) (GitHub - atw1020/sente: Sente is an open source python library for the game of Go), though this doesn’t affect move-by-move play. Overall, Sente is under active development (version 0.4+ released, with an active GitHub repository) but is relatively new, so its user community is smaller than older tools.

Sgfmill

Sgfmill is a robust Python 3 library focused on SGF game records and basic board operations, derived from the mature Gomill codebase (Introduction — Sgfmill 1.1.1 documentation). It can load SGF files into a Python object model (game tree of nodes), and can also apply moves to a board position while enforcing Go rules (Introduction — Sgfmill 1.1.1 documentation). In other words, Sgfmill handles both SGF parsing and move-by-move state tracking. It fully supports the SGF FF[4] standard for Go (but not other games) and has no external dependencies (GitHub - mattheww/sgfmill: Python library for Smart Game Format (SGF) files).

Strengths: Sgfmill excels at SGF parsing and manipulation. It provides convenient classes for SGF trees (e.g. Sgf_game, Tree_node) and can iterate over moves or variations easily. Comments and other properties are accessible as well. It’s reliable for converting a sequence of moves into a board position: one can start from an empty board and apply moves one by one, capturing stones and detecting illegal moves (ko) as needed (Introduction — Sgfmill 1.1.1 documentation). Documentation is thorough and covers examples of usage. Maintenance-wise, Sgfmill is stable; it is essentially the Python 3 port of Gomill’s SGF module (Introduction — Sgfmill 1.1.1 documentation) and saw its last release around 2018–2019. Even if not frequently updated now, it is a finished library that “just works” for SGF tasks.

Limitations: Sgfmill’s scope is narrower. It does not provide a full game engine interface or AI integration – it has no built-in way to connect to KataGo or any GTP engine. If using Sgfmill, requirement #3 would need to be handled by separate code (e.g. launching KataGo via subprocess and parsing its analysis output). Also, Sgfmill’s board representation is primarily meant for validation and move application; it doesn’t have advanced features beyond the essentials (for example, no graphical display – it’s text/logic only). Nonetheless, for requirements #1 and #2, Sgfmill is a reliable choice (it fully supports simulating captures/ko and reading game trees from SGF (Introduction — Sgfmill 1.1.1 documentation)). Given that it stems from Gomill (a well-tested toolkit) it is considered very trustworthy for SGF and rules correctness.

baduk (Macfergus’s Baduk)

baduk is a Python package (v1.0.4 on PyPI) that provides core Go game logic implemented in C++ with a Python wrapper. This library focuses on fast board state simulation – creating game states, playing moves, and checking rules. It was created to offer a performance boost over pure Python implementations. An example usage is simply: game = GameState.new_game(19) and then game.play_move(Point(x,y)), etc., to simulate a game. This library will enforce captures and ko, and can be used to determine move legality.

Strengths: The primary advantage of baduk is efficiency – heavy computations are done in C++, making it suitable for research if you need to iterate over many positions quickly. It covers board state rendering and rules (requirement #1) thoroughly. Its design is minimalistic, which can make it easy to integrate as a backend for custom research code that just needs a fast Go state engine.

Limitations: The library is quite limited in scope. It does not include SGF parsing or file I/O – you would have to feed moves into it yourself or write a parser (or use Sgfmill alongside it). It also has no built-in AI or GTP capabilities. To use KataGo with it, you’d again be writing your own interface. Another concern is the documentation and community: the author notes “the documentation is somewhat lacking” (Python 3 libraries for Go and GTP? • Life In 19x19), and the project (with around a dozen stars on GitHub) appears to be a one-person effort. Maintenance and support may be sparse, and you may have to dive into source if something goes wrong. In summary, baduk can handle requirement #1 with high performance, but you’ll need other tools for #2 and #3. It is best suited if you specifically need a fast rules engine and are prepared to handle SGF/AI integration separately.

KaTrain

KaTrain is actually a full application (a GUI program) for Go analysis based on KataGo, but it is installable via pip and contains reusable components. KaTrain uses KataGo’s analysis engine (not just the basic GTP interface) to provide rich analysis of positions (GitHub - sanderland/katrain: Improve your Baduk skills by training with KataGo!). It can load SGF files for review, step through the moves, and display the AI’s suggested variations, policy network evaluations, and win-rate estimates. Under the hood, KaTrain parses SGFs (it has a module for SGF parsing, sometimes known as pysgf) and manages a KataGo subprocess to query analysis for each position.

Strengths: KaTrain directly addresses requirement #3 – it was built to launch KataGo and retrieve neural network outputs. It can obtain the policy probabilities for all moves in a position, as well as value (win rate) and score estimates. This makes it extremely powerful for analyzing games or positions with minimal coding. In fact, KaTrain’s engine configuration uses KataGo’s analysis mode (via JSON communication) to get detailed information beyond what GTP alone provides (GitHub - sanderland/katrain: Improve your Baduk skills by training with KataGo!). KaTrain also inherently handles SGF files (you can load a game and navigate variations), fulfilling requirement #2. For board state (#1), KaTrain itself doesn’t need a separate rules engine because it trusts KataGo to evaluate moves; however, it still maintains the current board state internally to feed KataGo each move sequence. Ease-of-use for a researcher is mixed: as an interactive tool, it’s very user-friendly, and one could even use its GUI to visualize analysis. It is also actively maintained (tracking KataGo releases) due to an established user base.

Limitations: KaTrain is not a lightweight library but a complex application. Using it programmatically (as an imported module) is not straightforward — its code is geared toward driving a GUI and orchestrating analysis sessions, rather than exposing a simple API. Documentation is aimed at end-users, not developers. If one tries to repurpose KaTrain’s internals, they may have to read the source (which is reasonably organized, but not officially documented for API use). Another consideration is that KaTrain bundles a lot of extras (UI, training features) that may not be needed for pure research scripting, which could be overkill. In summary, KaTrain does support all three requirements (it can parse SGFs, show board states, and get KataGo policy outputs out-of-the-box), but treating it as a “library” requires extra effort. It might be more appropriate as a ready-made analysis tool to use alongside your code (for example, exporting AI analysis to files) rather than an integrated library in your Python codebase.

Other Notable Mentions

  • Gomill: The predecessor to Sente/sgfmill, Gomill is a Python 2 library with excellent GTP engine support and SGF utilities. It was widely used for coordinating matches between Go engines and parsing SGFs. However, it is not Python 3 compatible (gomill · PyPI) (gomill · PyPI) and no longer actively updated (last release in 2019). Sente was explicitly created to replace Gomill on Python 3 (GitHub - atw1020/sente: Sente is an open source python library for the game of Go), so for modern use Sente or Sgfmill are preferred.
  • PySGF (jtauber/sgf): A small SGF parsing library by John Tauber, available on PyPI as sgf. It provides a simple object model for SGF collections and games (GitHub - jtauber/sgf: Python implementation of Smart Game Format) (GitHub - jtauber/sgf: Python implementation of Smart Game Format). This can handle basic SGF needs, but it does not apply game rules or manage board state by itself (you’d still need a game engine to handle captures/ko if you replay moves). Its functionality is largely covered (with more features) by Sgfmill, so Sgfmill is usually the go-to choice for SGF in Python.
  • Other AI frameworks: Projects like Deep Learning and the Game of Go’s example code (BetaGo) or Facebook’s open-source ELF OpenGo are primarily about training AI, not providing general SGF/board utilities. They often have their own board implementations but are not packaged for reuse on PyPI. For example, BetaGo has SGF parsing and a Go board class, but it’s not actively maintained as a library.

Feature Support Comparison

The table below compares the key features of the discussed libraries against the requirements:

Library Board State Simulation (captures & ko) SGF Parsing & Game Tree KataGo Policy Integration Documentation & Support
Sente Yes (full rules engine) (GitHub - atw1020/sente: Sente is an open source python library for the game of Go) Yes (load/edit SGF) (GitHub - atw1020/sente: Sente is an open source python library for the game of Go) Partial – no built-in KataGo client (requires manual subprocess) (GTP Introduction — Sente 0.4.4 documentation) Good docs; active (Py3) development
Sgfmill Yes (apply moves with rules) (Introduction — Sgfmill 1.1.1 documentation) Yes (read/write SGF) (Introduction — Sgfmill 1.1.1 documentation) No – requires external engine integration Excellent SGF docs; stable but minimal recent updates
Baduk (C++) Yes (fast C++ core for rules) No (no SGF support) No – no engine integration Sparse docs (Python 3 libraries for Go and GTP? • Life In 19x19); small project
KaTrain Yes (uses KataGo to validate moves) Yes (loads SGF, navigates variations) Yes (built-in use of KataGo analysis engine) (GitHub - sanderland/katrain: Improve your Baduk skills by training with KataGo!) User-oriented docs; dev API not formalized, but maintained
Gomill Yes (rules and GTP support) Yes (SGF handling) Partial (can manage engines via GTP, but no neural net outputs) Deprecated (Python 2 only)

(Note: “Partial” under KataGo integration for Sente means it has the tools to incorporate KataGo (via GTP protocol handling), but the user must do the work of launching/communicating with the engine. Gomill could launch engines and parse their moves, but it predates neural-network AIs and does not fetch policy tensors.)

Recommendation

For most research purposes, the best solution is to combine a reliable Go game library for simulation/SGF with direct usage of KataGo’s analysis engine. In particular, Sente is a strong choice for requirements 1 and 2: it will let you simulate moves (ensuring captures and ko are handled correctly) and parse or manipulate SGF files easily (GitHub - atw1020/sente: Sente is an open source python library for the game of Go). Sente’s API is fairly high-level and well-documented, which is advantageous for research code maintenance. To fulfill requirement 3, you can launch KataGo in analysis mode (which outputs detailed JSON analysis for positions) and feed it board states obtained from Sente. This approach leverages each tool for what it does best: Sente for game logic and data handling, KataGo for AI analysis. The KataGo repository even provides example Python code for invoking the analysis engine (KataGo/docs/Analysis_Engine.md at master · lightvector/KataGo · GitHub), and using this you can retrieve the policy vector for any position (as well as win rate, etc.). While this does require writing some glue code, it gives you full control and access to KataGo’s neural network output in a scriptable way.

If you prefer not to mix tools, an alternative is KaTrain – it essentially already merges an SGF parser, a Go engine, and KataGo’s analysis. For a quick analysis of games, KaTrain can load an SGF and show policy values for each move out-of-the-box. However, as noted, using KaTrain programmatically is more cumbersome. It might be suitable if your research workflow can be designed around its existing interface (for example, having KaTrain generate analysis files that you then post-process). For more flexibility, though, Sente + KataGo will be easier to customize to specific research needs (e.g. analyzing arbitrary positions, running sweeps of hyperparameters on KataGo, etc.).

Summary of strengths: Sente offers a one-stop solution for simulation and SGF editing with good performance and Python 3 support (GitHub - atw1020/sente: Sente is an open source python library for the game of Go). Sgfmill is a focused and dependable SGF parser if your project is SGF-heavy. The baduk C++ library can be valuable if performance is critical and SGF support is handled elsewhere. KaTrain (with KataGo) is unparalleled for obtaining AI insights (policy/output distribution and evaluation) with minimal setup, since it directly uses KataGo’s analysis engine (GitHub - sanderland/katrain: Improve your Baduk skills by training with KataGo!).

Summary of limitations: Sente and Sgfmill lack direct AI integration – you must handle KataGo as an external component (which is feasible with a little work). KaTrain, while powerful, is not designed as a lean library; integrating it might introduce unnecessary complexity if you only need backend analysis. The baduk library, on the other hand, lacks high-level features (no SGF or AI) and has limited documentation (Python 3 libraries for Go and GTP? • Life In 19x19), meaning a steeper learning curve and possibly more effort to integrate with other tools.

In conclusion, a combination of Sente (for move sequencing and SGF handling) and KataGo’s analysis engine (invoked via Python subprocess or a thin wrapper) is recommended as the most suitable approach to cover all three requirements. This setup provides a balance of ease-of-use and completeness: you get a well-maintained library for the game mechanics and record-keeping, and you harness KataGo’s superior AI analysis capabilities in parallel. By using KataGo in analysis mode (rather than simple GTP), you can directly obtain the policy activations and other neural metrics for any given board state (KataGo/docs/Analysis_Engine.md at master · lightvector/KataGo · GitHub), which is ideal for research. If one prefers a more integrated solution and is willing to work within an existing tool’s framework, KaTrain is a viable option to consider, especially for quickly reviewing games with AI feedback. Overall, these libraries complement each other, and leveraging them in tandem can greatly accelerate Go-related research tasks while ensuring accuracy and depth of insight.

Sources:

1 Comment

Sign in or sign up to post a comment.

Peter de Blanc

22 days ago

I decided to go with KaTrain for now. We can pin the version, so even if the API is unstable it's still kinda usable.

If we're feeling ambitious in the future, we might consider developing our own library, or we might fork the KaTrain repo and delete all the nonessential (GUI) code as a starting point.

0