A Python library for reliably extracting structured JSON from text and validating it with Pydantic models.
Project description
LLMStruct
llmstruct is a Python library for reliably extracting structured JSON from text and validating it with Pydantic models.
Features
- Extracts JSON objects or arrays of JSON objects from messy text.
- Validates extracted JSON against Pydantic models.
- Resilient to surrounding text and other noise.
Installation
You can install the library using pip:
pip install .
Or with uv:
uv pip install .
Demos
This project includes two demo scripts to showcase its capabilities by interacting with the Anthropic API.
Prerequisites
Before running the demos, you need to install the demo-specific dependencies and set your Anthropic API key.
-
Install demo dependencies:
uv pip install -e ".[demos]"
-
Set the API Key: Export your Anthropic API key as an environment variable:
export ANTHROPIC_API_KEY="your-api-key"
Simple Demo
The simple demo shows the basic usage of extract_json_from_text to extract a single JSON object from a response generated by the Anthropic API.
To run the simple demo:
python demos/simple.py
Advanced Demo
The advanced demo illustrates a powerful, real-world use case. It performs the following steps:
- Fetches a large, complex JSON object for a product from the public Open Food Facts API.
- Sends this raw JSON to the Anthropic API and instructs the LLM to extract and structure a concise summary.
- Streams the LLM's response and uses
llmstructto parse the structured summary from the text.
The entire process is wrapped in a tenacity retry block, making the operation resilient to transient network issues or occasional LLM failures to generate a perfectly valid object on the first try. This demonstrates how llmstruct can be a key component in a robust data processing pipeline.
To run the advanced demo:
python demos/advanced.py
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmstruct-0.1.0.tar.gz.
File metadata
- Download URL: llmstruct-0.1.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.27
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
789e30de87af8fb4c892730a5b0ec9878a08ea8e05eb9ce58c263362e4fe8507
|
|
| MD5 |
37b5605e2520959cc8458c9be8b12cc3
|
|
| BLAKE2b-256 |
af947ea426eb70fbb793539b813af1d47735a7080fa8a5f1098debec5ac54464
|
File details
Details for the file llmstruct-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llmstruct-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.27
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71521de5724027f11ee46d18f2c6e8b45786d609972347772d6920f11d62cd56
|
|
| MD5 |
218ba7b7d88e24ff4f11960edff70a5d
|
|
| BLAKE2b-256 |
36f78dfe213761854180dea385b20b51a6b84282cb1cece5527f9ab722fa5f65
|