Skip to main content

Reliable, provider-agnostic structured outputs for production LLM applications.

Project description

StructGuard

Reliable, provider-agnostic structured outputs for production LLM applications.

StructGuard makes sure every LLM response conforms to a schema you define — Pydantic, dataclass, TypedDict, or raw JSON Schema — before it reaches your application code. It handles prompting, JSON repair, validation, and intelligent retries in one call, across OpenAI, Anthropic, Gemini, Groq, and Ollama.

Install

pip install structguard              # core only
pip install structguard[openai]      # + OpenAI SDK
pip install structguard[anthropic]   # + Anthropic SDK
pip install structguard[all]         # every provider SDK

Quick start

from pydantic import BaseModel
from openai import OpenAI
from structguard import generate

class Incident(BaseModel):
    summary: str
    priority: str
    confidence: float

client = OpenAI()

incident = generate(
    llm=client,
    prompt="Summarize this incident: Database connection pool exhausted, causing 500s.",
    schema=Incident,
)

print(incident.summary, incident.priority, incident.confidence)

With a full report

result = generate(llm=client, prompt=prompt, schema=Incident, return_report=True)
print(result.data)
print(result.report)   # ✓ Success | provider=OpenAI model=gpt-4o-mini retries=0 ...

Error handling

from structguard import RetryLimitExceeded, SchemaValidationError

try:
    incident = generate(llm=client, prompt=prompt, schema=Incident, max_retries=2)
except RetryLimitExceeded as e:
    print(f"Gave up after {e.attempts} attempts: {e.last_error}")

Supported schema types

  • pydantic.BaseModel
  • stdlib @dataclass
  • TypedDict
  • raw JSON Schema dict

Supported providers

OpenAI · Anthropic · Google Gemini · Groq · Ollama

Development

git clone https://github.com/sujanrupu/structguard
cd structguard
pip install -e ".[dev]"
pytest

License

MIT

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

structguard-0.1.4.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

structguard-0.1.4-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file structguard-0.1.4.tar.gz.

File metadata

  • Download URL: structguard-0.1.4.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for structguard-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f1ca1675c4127ba3edcaf7011d0cc86ebc1830c9f4595d7a1d1d98e85ef695f0
MD5 276151cabe12f88bc71a48efc8c44ec1
BLAKE2b-256 9bf9936c53c28e4dbf775603b75f5fcd11ede77faa5df43cdf3d6c1b1b372be5

See more details on using hashes here.

File details

Details for the file structguard-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: structguard-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for structguard-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 829e7d718c0e487318fcbdd2a94384590cc492e7abc0b03de2864d6558aeb12a
MD5 9ddefd9258b371b50a97962a965819b0
BLAKE2b-256 7292ca26e40118aa3780ba01e560853ab09977502558b82941322134bd99f4a8

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