Skip to main content

An open, more reliable architecture for parsing LLM output.

Project description

Phoenix: A Resilient Semantic Parser for LLM Output

PyPI Version License Python Versions

Getting reliable structured data from Large Language Models (LLMs) is a fundamental engineering challenge. When an LLM's output deviates even slightly from a strict JSON format, traditional parsers fail.

Phoenix is a lightweight, dependency-free library that intelligently extracts structured data from messy or incomplete LLM outputs. It doesn't just follow rules—it understands and recovers.


Key Features

  • Resilience Cascade: A three-layer defense system to ensure you always get a result.
  • Intelligent Cleaning: Automatically handles common LLM errors like comments, trailing commas, and "smart" quotes.
  • Semantic Recovery: As a last resort, it scans unstructured text to find key-value pairs and rebuilds the data from scratch.
  • Pydantic Integration: Leverages Pydantic for robust validation of the final output.
  • Lightweight & Fast: Zero dependencies besides Pydantic, ensuring easy integration.

How It Works: The Resilience Cascade

Phoenix processes LLM output through three layers of defense:

  1. Markdown Search: It first looks for JSON inside standard json ... code blocks.
  2. Direct Parsing & Cleaning: It then attempts to parse the output as clean JSON, automatically removing common syntax errors like comments.
  3. Semantic Extraction: If all else fails, Phoenix activates its superpower: it analyzes the raw, unstructured text, finds key: value pairs, and reconstructs the data.

Installation

pip install phoenix-parser

Quickstart

Here's how to use Phoenix to reliably parse data from a messy LLM output.

from phoenix_parser import AdaptiveSemanticParser, ParsingError
from pydantic import BaseModel, Field

# 1. Define your desired data structure using Pydantic
class UserProfile(BaseModel):
    user_name: str = Field(description="The full name of the user.")
    user_id: int = Field(description="The unique user identifier.")
    is_active: bool = Field(description="The active status of the user account.")

# 2. Get a messy, real-world output from an LLM
messy_llm_output = """
Here are the user details you requested.
// User is confirmed active.
```json
{
  “user_name”: “Alice”,
  “user_id”: "123", // ID is a string, needs to be an int!
  “is_active”: true, // Trailing comma...
}

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

3. Create a parser instance and parse the data

parser = AdaptiveSemanticParser()

try: # Phoenix will automatically clean, parse, and validate the data validated_data = parser.parse(messy_llm_output, UserProfile)

print("✅ Successfully parsed and validated!")
print(validated_data)
# Output: {'user_name': 'Alice', 'user_id': 123, 'is_active': True}

assert isinstance(validated_data['user_id'], int)

except ParsingError as e: print(f"❌ Failed to parse data: {e}")


---

## Comparison with Classic Parsers

| Feature               | Classic Parsers (e.g., `json.loads`)                      | The Phoenix Parser                                       |
|-----------------------|-----------------------------------------------------------|----------------------------------------------------------|
| **Technology**        | Rigid grammars                                            | Hybrid: Direct parsing + Semantic understanding          |
| **Flexibility**       | Low (breaks on syntax errors)                             | High (handles messy structures, comments, etc.)          |
| **Failure Response**  | Hard `JSONDecodeError`                                    | Semantic data recovery                                   |
| **Paradigm**          | "Fixing chaos with rules."                                | "Using intelligence to understand chaos."                |

---

## License

This project is licensed under the Apache 2.0 License. See the [LICENSE](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

phoenix_parser-0.5.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

phoenix_parser-0.5.0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file phoenix_parser-0.5.0.tar.gz.

File metadata

  • Download URL: phoenix_parser-0.5.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for phoenix_parser-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d7643d62ad58311709a386255e61ebc5cb2b507ebb5b2b559e42b47ed4c2fa68
MD5 7ade243ffbd6a72794bf2f4b42cba958
BLAKE2b-256 d3a915a63ca5e858917fc42d201798c2205c1c4a5e5a1d42098b5527225b63ee

See more details on using hashes here.

File details

Details for the file phoenix_parser-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: phoenix_parser-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for phoenix_parser-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 362c5d0b99e5e261b308953ef329e92bb356c33f4f18ed204a64ac10050c9a6c
MD5 7206e8c9bbc43e7c2a4864d334b307ce
BLAKE2b-256 2c3a31cb1f1706c8d8ede1b7e266239cc81ee4f36b1cc3d12415296aea2cb376

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