Registry / ai-ml / pynini

pynini

JSON →
library2.1.7pypypi✓ verified 87d ago

Pynini is a Python library providing efficient Python bindings for the OpenFst C++ library, enabling the construction, manipulation, and compilation of finite-state transducers (FSTs) and finite-state acceptors (FSAs). It's widely used for tasks in natural language processing (NLP) such as text normalization, phonology, and speech recognition. The current version is 2.1.7, with active development and regular releases.

pip install pynini
INSTALL
IMPORT
SIG · PYNINI
P
pynini
ai-mlpythonv2.1.7
Install
9.1s avg
Import
66ms
Disk
723MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.7 · 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
glibc
py 3.10
✕ build_error
✓ 7.25s
py 3.11
✕ build_error
✓ 10.98s
py 3.12
✕ build_error
✓ 7.85s
py 3.13
✕ build_error
✓ 10.13s
py 3.9
✕ build_error
3/4 runs
723MB installed
● package 723MB
Code
Verified usage

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

Fst
import pynini
Access as pynini.Fst to create or manipulate finite-state transducers.
string_file
import pynini
Access as pynini.string_file to compile OpenFst text files into FSTs.
union
import pynini
Access as pynini.union to create an FST that accepts the union of input FSTs or strings.
accep
import pynini
Access as pynini.accep to create an FSA (acceptor) from a string.
compose
import pynini
Access as pynini.compose to compose two FSTs.

This quickstart demonstrates creating basic finite-state acceptors and transducers, concatenating FSTs, and composing an input string with a mapping transducer to transform text.

import pynini as pn # Create a simple acceptor for 'hello' hello_fst = pn.accep("hello") # Create an acceptor for 'world' world_fst = pn.accep("world") # Concatenate them with a space acceptor using the overloaded '+' operator hello_world_fst = hello_fst + pn.accep(" ") + world_fst print(f"Hello World FST: {hello_world_fst}") print(f"Shortest path for Hello World: {pn.shortestpath(hello_world_fst).string()}") # Create a simple transducer mapping 'a' to 'b' a_to_b_map = pn.string_map([("a", "b"), ("c", "d")]) # Input string as an acceptor input_string_fst = pn.accep("apple_cake") # Compose the input with the transducer output_fst = pn.compose(input_string_fst, a_to_b_map) # Get the shortest path (result string) if not output_fst.empty(): print(f"'apple_cake' -> '{pn.shortestpath(output_fst).string()}'") else: print("No valid output path found.")
Debug
Known issues
gotchaPynini FSTs operate within specific weight semirings (e.g., Tropical, Log, Probability). The default is Tropical. Operations (like composition, union) on FSTs with different semirings will result in a runtime error.
fix
Ensure all FSTs involved in an operation use the same weight semiring. You can explicitly set the semiring when creating FSTs (e.g., `pn.Fst(weight_type="log")`) or convert them using `pn.cast()`.
affects: All versions
gotchaPynini FST objects are generally immutable. Operations like `compose()`, `union()`, `closure()`, `concat()` return new FST objects rather than modifying the original in place.
fix
Always assign the result of FST operations to a new variable or reassign to the original variable to capture the modified FST (e.g., `my_fst = pn.compose(my_fst, another_fst)`).
affects: All versions
gotchaDespite `pip install pynini` usually working, Pynini is a C++ library binding. On some Linux distributions, macOS versions, or for advanced compilation flags, you might encounter issues with the bundled OpenFst leading to linker errors or missing symbols.
fix
Ensure your system has necessary build tools (g++, make, etc.). For persistent issues, consider installing OpenFst manually according to its documentation and then compiling Pynini from source, ensuring it links to your specific OpenFst installation.
affects: All versions
gotchaWorking with very large grammars or performing complex FST operations can be memory and CPU intensive, potentially leading to slow execution or out-of-memory errors.
fix
Optimize grammar design by minimizing states/arcs where possible. Consider breaking down complex operations into smaller, manageable steps. Profile your code to identify bottlenecks and explore Pynini's various optimization flags or algorithms (e.g., `pn.optimize()`)
affects: All versions
Errors
Common errors & fixes
TypeError: unsupported operand type(s) for +: 'pynini.Fst' and 'str'
Attempting to concatenate a `pynini.Fst` object directly with a Python string.
fix
Convert the Python string into a finite-state acceptor using `pynini.accep()` before concatenation. Example: `my_fst + pynini.accep("literal_string")`.
pynini.exceptions.FstCompilerException: OpenFst compilation failed for ...
This error indicates that the underlying OpenFst library failed to compile the provided grammar or string. Common causes include malformed FST text syntax, invalid UTF-8 characters, or exceeding OpenFst's internal limits.
fix
Carefully examine the input string, text file, or arguments passed to functions like `pynini.string_map()` or `pynini.string_file()`. Ensure correct OpenFst FST text format, proper character escaping, and valid UTF-8 encoding. Test with smaller, simpler inputs to isolate the issue.
FileNotFoundError: No such file or directory: '...' (OpenFst)
Typically occurs when `pynini.string_file()` is called with a path to a non-existent file, or the Python process lacks read permissions for the specified file.
fix
Verify that the file path is absolutely correct and that the file exists at that location. Ensure the Python process has the necessary read permissions for the file. Use an absolute path or ensure the file is in the current working directory.
Upgrade
Version history
2.1.7latest on PyPI · released Sep 4, 2025
Audit
Dependencies
OpenFstoptionalPynini is a Python binding for the OpenFst C++ library. Pre-compiled wheels typically bundle OpenFst, but on some systems or for custom builds, OpenFst may need to be installed separately.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
pynini — pip install pynini · libregistry