Skip to main content

rgapi

rgapi is a Python API for ripgrep-style walking and search. It is meant for Python code that wants fd-style file discovery or rg-style searching without shelling out.

It uses the same ignore, grep-regex, and grep-searcher crates that ripgrep uses for walking, regex matching, and file scanning. Walking and searching run in parallel by default. Most expensive work stays in Rust.

Overview

For common file discovery and search:

from rgapi import fd, ls, rg, rg_iter

fd(".", ext="py", exclude="test_*.py")
ls("src")
for row in rg_iter("TODO", ".", include="*.py", context=2): print(row.asdict())
rg("TODO", ".", ext="py", skip_dir=".venv", paths=True)

For cell-aware search of Jupyter notebooks (see Notebooks):

from rgapi import nbrg

nbrg("read_csv", ".", cell_context=1)

Every walk and search has an async twin, plus streaming forms that yield results as they are found (see Async):

from rgapi import fda, rga, rga_iter, nbrga, nbrga_iter

await rga("TODO", ".", ext="py", timeout_ms=200)
async for row in rga_iter("TODO", "."): print(row)

For direct access to the regex, search, and walk pieces:

from rgapi import compile, search_path, search_text, walk

matcher = compile("TODO")
matcher.is_match("TODO")
matcher.finditer("TODO TODO")

walk(".")
search_text(matcher, "alpha\nTODO\nomega\n", path="memory.txt", context=1)
search_path(matcher, "src/lib.rs", display_path="src/lib.rs")

Install

pip install rgapi

Semantics

fd and walk return slash-separated paths relative to root. They use the ignore crate, so .gitignore, .ignore, and the usual ripgrep filters apply by default. .rgignore files are also honored and take precedence over .gitignore. Hidden files are skipped unless hidden=True. Pass ignore=False to disable all ignore filtering (including .rgignore). Symlinks are not followed unless follow_links=True; same_file_system=True avoids crossing filesystem boundaries. Traversal is parallel, and result order is not guaranteed; use sorted(...) if order matters. root arguments accept str or pathlib.Path and expand ~; ., ./, and paths containing .. work across the sync and async APIs.

fd adds fd-like filtering on top of walk: pattern is a smart-case regex matched against each basename, and include/exclude use glob syntax. Lowercase patterns match case-insensitively; a pattern containing uppercase letters is case-sensitive. Use path_re when matching the slash-separated relative path instead. glob= is accepted as an alias for include=. A basename glob such as *.py also matches recursively, so it finds src/app.py. Use ext="py" or ext=["py", "rs"] for extension filters, which compose as AND with include/glob (so include="src/*", ext="py" means src/* and *.py, like combining rg -g with -t); use min_depth=/max_depth= to bound recursion, and max_filesize= to skip files above a byte limit.

ls lists like the shell command: it is fd with defaults flipped to one level (max_depth=1), directories included, ignore rules off, and results sorted by name. hidden=True is ls -a, and every fd filter still applies.

fd_iter is the lazy form of fd, yielding FileEntry paths as the walk finds them, and takes every fd filter. It has no timeout_ms, since a consumer that stops asking for paths ends the walk itself.

path_re and skip_path_re are regex filters on slash-separated relative paths. They filter returned paths or searched files, but do not control traversal. skip_dir uses glob syntax to prune matching directory subtrees, and skip_dir_re does the same with regex.

rg and rg_iter return structured rows rather than raw CLI text. They accept the same include, exclude, glob, ext, path_re, skip_path_re, skip_dir, skip_dir_re, min_depth, max_depth, max_filesize, follow_links, and same_file_system filters as fd. Each row is a SearchLine with:

kind         'match', 'before', 'after', or 'context'
path         path relative to root
line_number  1-based line number
lnhash       exhash-style `lineno|hash|` address for the line
line         line text without the trailing newline
matches      list of (start, end) byte offsets for match rows

rg, search_text, and search_path return SearchResults by default, a list subclass whose str() and notebook pretty display are rg-style multiline text. rg_iter yields rows lazily.

SearchLine has a structured repr, an rg-style str (the line is truncated to 180 chars with a trailing for display; repr and asdict() keep the full line), and SearchLine.asdict() returns row fields as a plain Python dict. Pass rg(..., lnhashs=True) or rg_iter(..., lnhashs=True) to show lnhash addresses instead of line numbers in row display while keeping line_number available. rg(..., paths=True) returns unique matched paths, and rg(..., count=True) returns the total number of match spans. paths and count cannot both be set.

fd, walk, ls, and rg/nbrg with paths=True return PathResults, a list of FileEntry rows. A FileEntry is a str subclass holding the relative path, so all string uses keep working, and it stats itself lazily on first access: stat is a cached os.lstat result (None if the path has vanished), with size, mtime, and is_dir derived from it. A PathResults displays as an ls -l-style long listing, capped at rgapi.MAX_REPR rows with a final … N more line, so stats are read only for displayed rows; str() is still one plain path per line, and list(res) shows plain paths. rg(..., timeout_ms=200) and fd(..., timeout_ms=200) stop at the deadline and return whatever was collected by then; walk, ls, and the async forms take it too. Results record how they ended: stop_reason is None for a complete result, "max_results" when truncated by max_results, or "timeout" when a deadline hit, and complete is true when stop_reason is None. count=True returns a plain int, which cannot carry the flag, so it rejects timeout_ms.

before_context, after_context, and context are like rg -B, rg -A, and rg -C. Files containing NUL bytes or invalid UTF-8 are skipped.

Block summaries

rg(..., summary=True) returns one row per blank-line-delimited block instead of one row per matching line. Empty and whitespace-only lines delimit blocks. A block containing several matching lines appears once and keeps every matching SearchLine in matches.

rg("TODO", ".", summary=True, context=1, maxlen=120)

The result is BlockResults, a list of SearchBlock objects. Each block has path, block_index, start_line, end_line, start_lnhash, end_lnhash, kind, full source, and matches. Its display is path:start-end:source for matches and path:start-end-source for context. With lnhashs=True, the numeric range becomes copyable boundary addresses such as path:4|a3f2|,6|b1c3|:source. Embedded newline runs are shown as ; maxlen limits displayed source without changing source or asdict().

In summary mode, before_context, after_context, and context count neighbouring blocks. max_results counts matching blocks and retains their block context. summary=True cannot be combined with paths or count; it can be combined with lnhash when copyable block boundaries are useful.

Search is case-sensitive by default, matching rg. Use smart_case=True for rg --smart-case behavior, or case_sensitive=False to force case-insensitive matching.

Notebooks

nbrg searches Jupyter .ipynb files cell-by-cell, so results are cells rather than raw JSON lines, and each match is identified by its cell id (the nbformat cell/message id) rather than a line number. Searching a notebook with plain rg matches the escaped JSON text (including outputs and metadata) and reports meaningless JSON line numbers; nbrg instead searches each cell's reconstructed source and reports the cell id, which is stable across edits and points at the actual unit you work with.

from rgapi import nbrg

nbrg("read_csv", ".")                  # cells whose source matches, across all notebooks under "."
nbrg("read_csv", ".", cell_context=1)  # also include neighbouring cells as context

Notebooks are walked, parsed, and matched together in one parallel Rust pass, using the same regex engine as rg, so regex behaviour and the case_sensitive/smart_case flags match rg. Only cell source is searched, not outputs or metadata. nbrg accepts the same discovery filters as fd/rg (include, exclude, glob, hidden, max_depth, skip_dir, …).

nbrg returns NbResults, a list of NbCell. Each NbCell has:

path         notebook path relative to root
cell_index   0-based position of the cell in the notebook
cell_id      nbformat cell id (falls back to the cell index for notebooks without ids)
cell_type    'code', 'markdown', or 'raw'
kind         'match' or 'context'
source       full cell source
matches      list of SearchLine rows for the matched lines within the cell

NbCell.asdict() returns those fields as a plain dict (with matches as SearchLine dicts). str() and pretty display show one line per cell, newline runs shown as , keyed by cell_id: path:cell_id:source for matches and path:cell_id-source for context. A match row starts at its first matched line: earlier lines display as …[Ln] (n the matched line's 1-based number in the cell), except that a leading #| directive line is kept, e.g. #| export…[L4]needle here. maxlen controls the displayed source length and defaults to 120; the full source remains in source and asdict(). A cell with several matches appears once, with every hit collected in matches.

cell_context=N includes the N cells before and after each matching cell as kind="context" rows (deduplicated per notebook).

nbrg_iter yields NbCell rows lazily as notebooks are parsed. nbrg also accepts max_results (at most that many cells, after sorting by path and cell index), count=True (number of matching cells), and timeout_ms= with the same stop_reason semantics as rg.

Notebook walking, parsing, and matching all happen in parallel in Rust, in the same pass as the file walk. Parsing uses a lean model that reads only each cell's id, cell_type, and source and skips outputs and metadata, so large embedded outputs (images, plots) are never materialized. search_nb(pattern, path, ...) searches a single notebook file the same way.

Async

fda, rga, and nbrga are awaitable twins of fd, rg, and nbrg, and fda_iter, rga_iter and nbrga_iter are async generators that yield rows as the search finds them. All take the same arguments and return the same types as their sync counterparts.

from rgapi import fda, rga, rga_iter

await fda(".", ext="py")
res = await rga("TODO", ".", timeout_ms=200)
if not res.complete: print(f"partial results: {res.stop_reason}")
async for row in rga_iter("TODO", "."): ...

None of this uses asyncio.to_thread or the loop's executor. The walk and search run on Rust threads, and a single callback settles the awaited future (or feeds the generator's queue) through loop.call_soon_threadsafe, so the event loop never blocks and contextvars behave normally.

Cancellation cleans up the Rust workers automatically. Wrapping a call in asyncio.wait_for or asyncio.timeout, cancelling the task (as starlette does when a client disconnects), or leaving an async for early all stop the search within about one row. One caveat comes from the language rather than the library: break inside async for only finalizes the generator at GC time, so for prompt cleanup wrap the iterator in contextlib.aclosing:

async with aclosing(rga_iter("TODO", ".")) as it:
    async for row in it:
        if enough(row): break

The streaming forms suit incremental display, such as pushing each batch of results to a browser as it arrives. The collected forms with timeout_ms give the best results available within a budget, and asyncio.wait_for gives timeout-as-failure. Pick per call site.

Benchmarks

tools/bench.py compares the rg CLI with in-process rgapi. Run it against a release build. One run on this machine, using best time from seven repeats:

fixture rg rgapi
6 x 2 MB files, 2 matches 6.54 ms 1.44 ms
800 x 1.5 KB files, 2 matches 13.90 ms 10.94 ms
tiny dir, repeated 30x 5.92 ms 2.14 ms

Release files for rgapi 0.1.20

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rgapi 0.1.20
File Size Uploaded
rgapi-0.1.20.tar.gz 50.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for rgapi 0.1.20
File
rgapi-0.1.20-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
rgapi-0.1.20-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
rgapi-0.1.20-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
rgapi-0.1.20-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
rgapi-0.1.20-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
rgapi-0.1.20-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
rgapi-0.1.20-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
rgapi-0.1.20-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details

Total release size: 13.4 MB

Release files / rgapi-0.1.20.tar.gz

Download URL rgapi-0.1.20.tar.gz
Size 50.4 kB
Tags Source
SHA-256 checksum
How to use checksums
10afc1559bf67e73607137b3415351ed59467c8f8bda1a1ba11e37bad588fd0a
BLAKE2b-256 checksum
How to use checksums
a6a78577d3c1fa1a38dd1aa3a35cae6ce6bbfaca4a07aa91d160846e1a054e3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rgapi-0.1.20-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.8 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
c06fa419eb3d105cdf6c3ed4f4c0a513ef29571fe010b58c78b67acdd9125ffc
BLAKE2b-256 checksum
How to use checksums
7e1061f5bf64bc3184e8779e4f045e6f7303dc79a3aac5c6cfc1a14fb389c789
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp313-cp313-macosx_11_0_arm64.whl

Download URL rgapi-0.1.20-cp313-cp313-macosx_11_0_arm64.whl
Size 1.6 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
433eb5ac8e2840b24dfcea1c9f98f0cfa79d16cb450220a0276570b78dfb450c
BLAKE2b-256 checksum
How to use checksums
8d6b7749cc5fe0f7cd816ebe6bd1bb7496daae88406260168675de4ba679141a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rgapi-0.1.20-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.8 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f5f35479dbdf2c8e9b5be44fc1d329a1ce4c676ce5ccf5962a26194fcb5e52fb
BLAKE2b-256 checksum
How to use checksums
fc058948ca9e057f6da7b20f5e1a7dcb015cf5ea4b622949f50827d13d8f46cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp312-cp312-macosx_11_0_arm64.whl

Download URL rgapi-0.1.20-cp312-cp312-macosx_11_0_arm64.whl
Size 1.6 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d9a41631c3215030036b1a29c3685b5e9ffde5e522ae4218355880d0c3e13514
BLAKE2b-256 checksum
How to use checksums
d0ecc8d18e239aa10b85ed08588aca85f715a3b2f8139a2fa6423080e5a30fa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rgapi-0.1.20-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e95d85951d85abfe3cd3b582aeeccbcd745f1d962a7670c21172fceaaf892e7b
BLAKE2b-256 checksum
How to use checksums
8838ff39415c97a5606504e77021b03e6af37dca0fd2f00ad011904df5fc6e7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp311-cp311-macosx_11_0_arm64.whl

Download URL rgapi-0.1.20-cp311-cp311-macosx_11_0_arm64.whl
Size 1.6 MB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
94f67cdf7ed20f2ad82db192989a8188e3772c915d6d52e4ba11624b43d26f66
BLAKE2b-256 checksum
How to use checksums
25a73f3a030cceaa1c7ee953aa6e8540e4421a9b02d46ebe8dcc61f015337532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rgapi-0.1.20-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.8 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
151d66c23bdd56c8ef8f769c7241367d9f310d5631c79accd413a56a898ea214
BLAKE2b-256 checksum
How to use checksums
fcb712e642267240040ad0056248888b6cf8139768f2a67715880f098e06335a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / rgapi-0.1.20-cp310-cp310-macosx_11_0_arm64.whl

Download URL rgapi-0.1.20-cp310-cp310-macosx_11_0_arm64.whl
Size 1.6 MB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b252dc15c16fd0917d2bebe5527fd270f7e55a337c706d4ad81fa1ca1fda43c3
BLAKE2b-256 checksum
How to use checksums
148a516aab89d5e835360cf135c2965a94c836387d2a4ee67a74e70b5c17e331
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.21

9 release files

This release

0.1.20 This release

9 release files

0.1.19

9 release files

0.1.18

9 release files

0.1.17

9 release files

0.1.16

9 release files

0.1.15

9 release files

0.1.14

9 release files

0.1.11

9 release files

0.1.10

9 release files

0.1.9

9 release files

0.1.8

9 release files

0.1.6

9 release files

0.1.5

9 release files

0.1.4

9 release files

0.1.2

9 release files

0.1.1

9 release files

0.1.0

9 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page