Metadata-Version: 2.4
Name: llmstruct
Version: 0.1.0
Summary: A Python library for reliably extracting structured JSON from text and validating it with Pydantic models.
Author-email: Jan Philip Göpfert <janphilip@gopfert.eu>
License: MIT License
        
        Copyright (c) 2024 Jan Philip Göpfert
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Requires-Python: >=3.13
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pydantic>=2.11.5
Provides-Extra: dev
Requires-Dist: pytest>=8.3.5; extra == "dev"
Provides-Extra: demos
Requires-Dist: tenacity>=9.1.2; extra == "demos"
Requires-Dist: anthropic>=0.28.0; extra == "demos"
Requires-Dist: httpx>=0.27.0; extra == "demos"
Dynamic: license-file

# 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`:

```bash
pip install .
```

Or with `uv`:
```bash
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:**
    ```bash
    uv pip install -e ".[demos]"
    ```

2.  **Set the API Key:**
    Export your Anthropic API key as an environment variable:
    ```bash
    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:

```bash
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:

```bash
python demos/advanced.py
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
