Skip to main content

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.

  1. Install demo dependencies:

    uv pip install -e ".[demos]"
    
  2. 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:

  1. Fetches a large, complex JSON object for a product from the public Open Food Facts API.
  2. Sends this raw JSON to the Anthropic API and instructs the LLM to extract and structure a concise summary.
  3. Streams the LLM's response and uses llmstruct to 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llmstruct-0.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llmstruct-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

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

Hashes for llmstruct-0.1.0.tar.gz
Algorithm Hash digest
SHA256 789e30de87af8fb4c892730a5b0ec9878a08ea8e05eb9ce58c263362e4fe8507
MD5 37b5605e2520959cc8458c9be8b12cc3
BLAKE2b-256 af947ea426eb70fbb793539b813af1d47735a7080fa8a5f1098debec5ac54464

See more details on using hashes here.

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

Hashes for llmstruct-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71521de5724027f11ee46d18f2c6e8b45786d609972347772d6920f11d62cd56
MD5 218ba7b7d88e24ff4f11960edff70a5d
BLAKE2b-256 36f78dfe213761854180dea385b20b51a6b84282cb1cece5527f9ab722fa5f65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page