Metadata-Version: 2.4
Name: llm-output-parser
Version: 0.1.0
Summary: Extract and parse JSON from unstructured text outputs from LLMs
Home-page: https://github.com/KameniAlexNea/llm-output-parser
Author: Alex Kameni
Author-email: Kamenialexnea@gmail.com
Keywords: llm,json,parsing,extraction,nlp,ai,language models
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Markup
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license-file
Dynamic: requires-python
Dynamic: summary

# LLM-Output-Parser

A robust utility for extracting and parsing JSON from unstructured text outputs generated by Large Language Models (LLMs).

## Features

- Extracts JSON from plain text, code blocks, and mixed content
- Handles various JSON formats: objects, arrays, and nested structures
- Advanced extraction strategies for multiple JSON objects in text
- Provides robust error handling and recovery strategies
- Works with markdown code blocks (``json ... ``)
- Intelligently selects the most comprehensive JSON when multiple are found

## Installation

Install from PyPI:

```bash
pip install llm-output-parser
```

Or install from source:

```bash
git clone https://github.com/KameniAlexNea/llm-output-parser.git
cd llm-output-parser
pip install -e .
```

## Usage

### Basic Usage

```python
from llm_output_parser import parse_json

# Parse JSON from an LLM response
llm_response = """
Here's the data you requested:


{
    "name": "John Doe",
    "age": 30,
    "skills": ["Python", "Machine Learning", "NLP"]
}


Let me know if you need anything else!
"""

data = parse_json(llm_response)
print(data["name"])  # John Doe
print(data["skills"])  # ['Python', 'Machine Learning', 'NLP']

```

### Handling Complex Cases

The library can handle various complex scenarios:

#### JSON Within Text

```python
text = 'The user profile is: {"name": "John", "email": "john@example.com"}'
data = parse_json(text)  # -> {"name": "John", "email": "john@example.com"}
```

#### Multiple JSON Objects

When multiple valid JSON objects are present, the parser returns the most comprehensive one:

```python
text = '''
Small object: {"id": 123}

Larger object:
{
    "user": {
        "id": 123,
        "name": "John",
        "email": "john@example.com",
        "preferences": {
            "theme": "dark",
            "notifications": true
        }
    }
}
'''
data = parse_json(text)  # Returns the larger, more complex object
```

#### JSON with Comments or Trailing Commas

The parser can handle some common JSON syntax errors:

```python
text = '''
{
    "name": "Test", // This is a comment
    "values": [1, 2, 3,],  // Trailing comma
    "settings": {
        "active": true,
    }
}
'''
data = parse_json(text)  # Cleaned and parsed successfully
```

## Error Handling

If no valid JSON can be found, a `ValueError` is raised:

```python
try:
    data = parse_json("No JSON here!")
except ValueError as e:
    print(f"Error: {e}")  # "Error: Failed to parse JSON from the input string."
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License

This project is licensed under the MIT License - see the LICENSE file for details.
