Observed since 21 Oct, 10:40 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
ai-extract
Extract structured data from any AI. Fast. Simple. Reliable.
Extract structured data from any AI response. Fast. Simple. Reliable.
ai-extract is a fast, lightweight Python library for extracting JSON from AI outputs. It handles markdown fences, surrounding text, and multiple JSON blocks.
orjson for 10x faster JSON parsingpip install ai-extract
from ai_extract import extract_json
# Extract from messy AI output
text = """
Here's the JSON you requested:
```json
{"name": "John", "age": 30}
Hope this helps! """
data = extract_json(text) print(data) # {'name': 'John', 'age': 30}
## Usage Examples
### Basic Extraction
```python
from ai_extract import extract_json
# Pure JSON
data = extract_json('{"key": "value"}')
# JSON in text
data = extract_json('The result is: {"success": true}')
# JSON in markdown fence
data = extract_json('```json\n{"data": [1,2,3]}\n```')
# Get first JSON (default)
data = extract_json('{"a": 1} and {"b": 2}')
# Returns: {'a': 1}
# Get all JSON blocks
data = extract_json('{"a": 1} and {"b": 2}', strategy="all")
# Returns: [{'a': 1}, {'b': 2}]
from ai_extract import extract_json, ExtractError
# Raise exception (default)
try:
data = extract_json("no json here")
except ExtractError as e:
print(f"Error: {e.message}")
print(f"Type: {e.error_type}")
# Return None instead
data = extract_json("no json here", raise_on_error=False)
# Returns: None
# From argument
ai-extract '{"key": "value"}'
# From file
ai-extract -f response.txt
# From stdin
echo '{"key": "value"}' | ai-extract
# Pretty print
ai-extract -f response.txt --pretty
# Get all JSON blocks
ai-extract -f response.txt --all
# Show metadata
ai-extract -f response.txt --verbose
extract_json(text, *, strategy="first", raise_on_error=True)Extract JSON from text.
Parameters:
text (str): Text containing JSONstrategy (str): How to handle multiple JSON blocks
"first": Return first valid JSON (default)"all": Return list of all JSON blocksraise_on_error (bool): Raise ExtractError on failure (default: True)Returns: Parsed JSON data, or list if strategy="all", or None if raise_on_error=False
The library tries multiple strategies in order:
MIT License - see LICENSE for details.
Rahul Vishwakarma (@rvv-karma)
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
orjsonEnvironment{
"extras": "",
"marker": null,
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | orjson>=3.9.0 | runtime |
pytest-benchmarkEnvironment{
"extras": "",
"marker": "extra == \"bench\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | pytest-benchmark>=4.0; extra == "bench" | runtime |
hypothesisEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | hypothesis>=6.0; extra == "dev" | runtime |
mypyEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | mypy>=1.0; extra == "dev" | runtime |
pytest-covEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | pytest-cov>=4.0; extra == "dev" | runtime |
pytestEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | pytest>=7.0; extra == "dev" | runtime |
ruffEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | ruff>=0.1.0; extra == "dev" | runtime |
mkdocs-materialEnvironment{
"extras": "",
"marker": "extra == \"docs\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | mkdocs-material>=9.0; extra == "docs" | runtime |
mkdocstringsEnvironment{
"extras": "python",
"marker": "extra == \"docs\"",
"requires_python": ">=3.10",
"evaluation": "NOT_EVALUATED"
} | mkdocstrings[python]>=0.24; extra == "docs" | runtime |
bdist_wheel · 9.67 KB · Python py3
sdist · 15.52 KB · Python source
Observed since 21 Oct, 10:40 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:ba16c821aa60c17b7158f761d3e48dbf2f9c475d6ff32caea0fdefcd2843ee2e