Registry / serialization / tree-sitter

tree-sitter

JSON →
library0.26.0pypypi✓ verified 26d ago

Tree-sitter provides Python bindings to the core Tree-sitter parsing library, enabling fast, incremental parsing and the generation of concrete syntax trees for various programming languages. It's actively maintained, with frequent updates (minor and patch releases typically every few weeks or months) to keep pace with the underlying C library. The current version is `0.25.2`.

pip install tree-sitter
INSTALL
IMPORT
SIG · TREE-SITTER
T
tree-sitter
serializationpythonv0.26.0
Install
1.6s avg
Import
11ms
Disk
42MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.26.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.012s · 66.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.008s · 20MB
42MB installed
● package 42MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Language
from tree_sitter import Language
Parser
from tree_sitter import Parser
Query
from tree_sitter import Query
QueryCursor
from tree_sitter import QueryCursor
language
import tree_sitter_python as tspython; PY_LANGUAGE = Language(tspython.language())
from tree_sitter_python import language; PY_LANGUAGE = Language(language())
It's best practice to import the language module with an alias and then access its `language()` function to avoid name collisions and for clarity. Direct import of `language` is also common but less explicit.

This quickstart demonstrates how to load a language grammar (Python in this case), create a parser, parse a code string into a syntax tree, and then use Tree-sitter's query system to find specific patterns within the tree, such as function names. It highlights the typical workflow of initializing a `Language` object from a pre-compiled grammar and then using a `Parser` to process source code. Ensure the relevant language package (e.g., `tree-sitter-python`) is installed alongside `tree-sitter`.

import os from tree_sitter import Language, Parser # NOTE: For this to work, you need 'tree-sitter-python' installed # pip install tree-sitter-python import tree_sitter_python as tspython # Load the Python language grammar PY_LANGUAGE = Language(tspython.language()) # Create a parser and set its language parser = Parser() parser.set_language(PY_LANGUAGE) # Source code to parse code = b""" def greet(name): # type: (str) -> None print(f"Hello, {name}!") greet("World") """ # Parse the code tree = parser.parse(code) # Get the root node and print its type root_node = tree.root_node print(f"Root node type: {root_node.type}") # Traverse the tree (example: find function definitions) query_string = """ (function_definition name: (identifier) @function.name) """ query = PY_LANGUAGE.query(query_string) captures = query.captures(root_node) for node, name in captures: print(f"Found function: {node.text.decode('utf8')} (capture: {name})")
Debug
Known issues
breakingThe `Language` constructor no longer accepts a raw pointer (`int`) directly but expects a capsule object. If you were dynamically loading a `.so` file, you might need to use `ctypes.CDLL` and extract the language function or adjust your loading mechanism. Additionally, `Parser.parse()` no longer accepts a `keep_text` argument, and range arguments for `Query.captures()` and `Query.matches()` have been removed; use `query.set_byte_range()` or `query.set_point_range()` instead.
fix
For custom language loading, ensure you're passing a proper `PyCapsule` or using a language binding that provides it. Remove `keep_text` from `parser.parse()` calls. For queries, pre-set the desired range using `query.set_byte_range()` or `query.set_point_range()` before calling `captures()` or `matches()`.
affects: >=0.25.0
breakingPython 3.9 is no longer supported; the library now requires Python 3.10 or newer. The `Language(ptr: int)` constructor was deprecated, urging users to use language binding packages instead of raw pointers. The method `Node.child_containing_descendant` was also deprecated.
fix
Upgrade your Python environment to 3.10 or later. Migrate away from `Language(ptr: int)` by using official language binding packages (e.g., `tree-sitter-python`) which provide a `language()` function. Update code using `Node.child_containing_descendant` to equivalent newer methods or tree traversal logic.
affects: >=0.24.0
gotchaIncompatibilities can arise between the `tree-sitter` Python package and specific `tree-sitter-<language>` grammar packages if their underlying Tree-sitter ABI versions differ. This can lead to runtime errors when loading languages.
fix
Ensure that the `tree-sitter` package and any `tree-sitter-<language>` packages you use are compatible by checking their release notes or by installing versions known to work together. If encountering issues, try aligning their versions, typically by upgrading/downgrading one or both packages.
affects: All versions
gotchaHigh memory usage has been reported in certain scenarios, particularly when parsing very large files or using complex grammars, potentially indicating memory leaks in specific contexts or substantial resource requirements of the core library.
fix
Monitor memory usage closely in production. For large inputs, consider processing files in chunks or optimizing your queries to reduce the scope. Report any persistent, unexplainable memory growth as a bug to the project maintainers, providing a minimal reproducible example.
affects: All versions
gotchaWhile many languages have `tree-sitter-<language>` packages, for less common languages or custom grammars, you might need to manually compile the grammar into a shared library (`.so` or `.dll`) and then load it. The `tree-sitter-languages` package, which simplifies loading, is currently unmaintained and may have compatibility issues with newer `tree-sitter` versions.
fix
For unsupported languages, follow the official Tree-sitter documentation for compiling custom grammars. Then, use `from tree_sitter import Language; MY_LANGUAGE = Language(f'path/to/my_language.so', 'my_language_name')` to load it, ensuring the ABI version matches. Avoid `tree-sitter-languages` in new projects.
affects: All versions
breakingThe `tree_sitter.Parser` object reports an `AttributeError` indicating it lacks the `set_language` method. This method is expected to be present in `tree-sitter` version 0.23.2, suggesting a potential issue with the package installation, corruption, or an unexpected environment interaction preventing the method from being exposed.
fix
Verify the integrity of your `tree-sitter` installation by reinstalling it (`pip install --force-reinstall tree-sitter`). Ensure there are no conflicting packages or unusual environment configurations that might interfere with the package's functionality. If the issue persists, consider trying a different minor version of `tree-sitter` or consulting the project's issue tracker for known environment-specific problems.
affects: 0.23.2
breakingThe `Parser.set_language()` method has been removed.
fix
Initialize the `Parser` with the `language` argument (e.g., `parser = Parser(language=MY_LANGUAGE)`) or assign the language directly to the `language` property (e.g., `parser.language = MY_LANGUAGE`).
affects: >=0.20.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'tree_sitter'
The `tree-sitter` Python package is not installed in your current environment or is not accessible.
fix
pip install tree-sitter
error: Microsoft Visual C++ 14.0 or greater is required.
Building Tree-sitter language grammars or compiling the `py-tree-sitter` package requires a C/C++ compiler, which is missing on Windows.
fix
Install the "Build Tools for Visual Studio 2022" (or 2019) from Microsoft's website, ensuring to select the 'Desktop development with C++' workload during installation.
FileNotFoundError: [Errno 2] No such file or directory: 'build/my-language.so'
The compiled shared library for the Tree-sitter language grammar could not be found, likely because `tree_sitter.Language.build_library()` failed or the specified path is incorrect.
fix
Ensure `tree_sitter.Language.build_library()` successfully compiles the grammar, verify the `output_path` matches where you later try to load it, and confirm the `source_paths` are correct.
AttributeError: 'Parser' object has no attribute 'set_language'
You are attempting to use an outdated method (`set_language`) on the `tree_sitter.Parser` object; the API has changed.
fix
parser.language = language
Upgrade
Version history
0.26.0latest on PyPI · released Jun 30, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or newer.
tree-sitter-<language>optionalSpecific language grammars are often provided as separate packages (e.g., `tree-sitter-python`) for pre-compiled binaries.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
tree-sitter — pip install tree-sitter · libregistry