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.2.tar.gz (15.2 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.2-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: parsec_llm-0.1.2.tar.gz
  • Upload date:
  • Size: 15.2 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.2.tar.gz
Algorithm Hash digest
SHA256 c5c08a3c91334edd6e5c2ef90868254142059f084a70c94c58f68d48bdf889e0
MD5 b61c46705b9c052ed6eb5273cf99bb10
BLAKE2b-256 db5a73ae760aa8f751430e05fa84a25144f440378350ab9c1cacc7d996fea364

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parsec_llm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.4 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0c71ceca1788829a4d7cabd04bd03a6bcb2952f4a372e4b4778b01e3650deafb
MD5 f48b4247efb9f39952d3956445c28935
BLAKE2b-256 afcd28ab0a4f744c4a9c812b18a9945cb942c62dd0ee9204834b105ec1e125a6

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