Skip to main content

Lightweight orchestration toolkit to generate, validate, repair and enforce structured output from LLMs

Project description

parsec

Tests License: MIT Python 3.9+

Lightweight orchestration toolkit to generate, validate, repair and enforce structured output from large language models (LLMs). The project provides a provider-agnostic adapter interface, validators (JSON/pydantic), and an enforcement engine that retries and repairs LLM output until it conforms to a schema.

This repository contains:

  • Adapter abstractions and a concrete OpenAI adapter.
  • Validation and repair utilities for JSON output.
  • An EnforcementEngine that generates, validates, repairs, and retries.
  • Examples and tests demonstrating usage.

Features

  • Provider-agnostic adapter interface for plugging different LLMs.
  • Native-structured output support (when providers allow JSON responses).
  • JSON validation with schema-based repair heuristics.
  • Retry loop with feedback to the model for progressive repair.
  • Small test suite and example runner using the OpenAI adapter.

Installation

pip install parsec-llm

Or for development:

git clone https://github.com/olliekm/parsec.git
cd parsec
pip install -e ".[dev]"

Quick Example

from parsec.models.adapters import OpenAIAdapter
from parsec.validators import JSONValidator
from parsec.enforcement import EnforcementEngine

# Set up components
adapter = OpenAIAdapter(api_key="your-api-key", model="gpt-4o-mini")
validator = JSONValidator()
engine = EnforcementEngine(adapter, validator)

# Define your schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"}
    },
    "required": ["name"]
}

# Enforce structured output
result = await engine.enforce(
    "Extract: John Doe is 30 years old",
    schema
)

print(result.parsed_output)  # {"name": "John Doe", "age": 30}

Development Setup

Requirements: Python 3.9+

  1. Install dependencies:
pip install -e ".[dev]"
  1. Run tests:
poetry run pytest -q
  1. Run the OpenAI example (requires OPENAI_API_KEY):
export OPENAI_API_KEY="sk-..."
export OPENAI_MODEL="gpt-4o-mini"  # optional
poetry run python examples/run_with_openai.py

The example demonstrates using OpenAIAdapter, JSONValidator and EnforcementEngine to extract structured data using a JSON schema.

Code Structure

  • src/parsec/core — core abstractions and schemas.
  • src/parsec/models — provider adapters (OpenAI, Anthropic).
  • src/parsec/validators — validator implementations.
  • src/parsec/enforcement — enforcement/orchestration logic.
  • src/parsec/cache — caching implementations.
  • src/parsec/utils — utility functions (partial JSON parsing).
  • examples/ — example runners (real OpenAI example included).
  • tests/ — unit tests with pytest.

Testing

Run the test suite with:

poetry run pytest -q

Notes & Next Steps

  • The OpenAI example performs real API calls — be mindful of API keys and costs when running it.
  • Consider mocking adapters for offline or CI-safe tests.
  • This project is intentionally minimal and modular — adapters and validators can be extended to support additional providers and formats.

License

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

Copyright (c) 2025 Oliver Kwun-Morfitt

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

parsec_llm-0.1.3.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

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

parsec_llm-0.1.3-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file parsec_llm-0.1.3.tar.gz.

File metadata

  • Download URL: parsec_llm-0.1.3.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for parsec_llm-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1ba2ae5a9659cf55a1c254b558f17f3c6d3caba0e2df79fc2a5ff8b3133e861e
MD5 8028f20c405936c761af10e2986599e1
BLAKE2b-256 c019703a15aaa22c3903e71d1f34f27bd02e9b4730d3b5643cb3e5a68d8f5593

See more details on using hashes here.

File details

Details for the file parsec_llm-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: parsec_llm-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for parsec_llm-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 41a61db14fac3ff71f9f6d7b5ab63dc87f1441c06280898bae2b5922a6151ef8
MD5 1fee3313a9b4d41d19fb2c9ce42e1fca
BLAKE2b-256 fe9491ec1809e5dda979a70821ec7f69decb02427c822c7589d56980a3a870d2

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