Skip to main content

LLM validation wrapper

Project description

Heimdall

Heimdall is a LangGraph-compatible utility for validating and correcting structured outputs from Large Language Models (LLMs) against Pydantic models. It automates error handling, correction, and validation for YAML/JSON outputs, making it easy to enforce schema compliance in LLM workflows.

Features

  • Automated Validation: Checks LLM outputs against Pydantic models.
  • Error Correction Loop: Automatically reflects on and corrects formatting/validation errors.
  • Modular Design: Easily integrates as a LangGraph subgraph or standalone validator.
  • Flexible LLM Support: Works with any LangChain-compatible LLM (e.g., OpenAI, VertexAI, Ollama).
  • Customizable Prompts: Uses configurable correction and reflection prompts.

Installation

pip install heimdall

Usage

from heimdall import structured_output_validator, HeimdallState
from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI

# Define your schema
class MyData(BaseModel):
    name: str = Field(description="Person's name")
    age: int = Field(description="Person's age")

# Initialize LLMs
main_llm = ChatOpenAI(model="gpt-3.5-turbo")
correction_llm = ChatOpenAI(model="gpt-4")

# Create the validator graph
validator = structured_output_validator(
    pydantic_model=MyData,
    llm=main_llm,
    thinking_model=correction_llm
)

# Prepare initial state
state = HeimdallState(
    messages=[("human", "My name is Bob and I am 30 years old. Format this.")],
    llm_output="",
    error_status=False,
    error_description="",
    iterations=0
)

# Run validation
result = validator.invoke(state)
print(result['llm_output'])  # {'name': 'Bob', 'age': 30}

API

structured_output_validator Creates a LangGraph graph object for structured output validation and correction.

Arguments:

  • pydantic_model: Pydantic model for output validation.
  • llm: Main LLM (LangChain Runnable).
  • thinking_model: (Optional) Stronger LLM for corrections.
  • callbacks, trace_id, parser: (Optional) Advanced configuration.

Returns:

A compiled LangGraph graph (Runnable) for validation.

heimdall_graph Convenience wrapper for single-call validation.

Contributing

Contributions are welcome! Please open issues or pull requests on GitHub.

License

MIT License. See LICENSE for details.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pst_heimdall-1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

pst_heimdall-1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file pst_heimdall-1.0.tar.gz.

File metadata

  • Download URL: pst_heimdall-1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pst_heimdall-1.0.tar.gz
Algorithm Hash digest
SHA256 827e53a8f57b3fbc27770e10ff600d3aed89e78f06c85f2ab3c4ec2c1b2608af
MD5 6ae86b55e11a11ff714e04c42af5f53f
BLAKE2b-256 03c82321d21025a5f50d1a5c9ab08642332cbd0d02c7be09d88c540b46e18636

See more details on using hashes here.

File details

Details for the file pst_heimdall-1.0-py3-none-any.whl.

File metadata

  • Download URL: pst_heimdall-1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pst_heimdall-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 953ba09c571a34ae331f519f6053423667d64e63cb0725276cac74ce0649549f
MD5 7d91902f1065bffdcaf9162525dda473
BLAKE2b-256 03a912a0442a5b34d4395f8f64997a6c63c8419fdda27d573ac5c92cb4bb48b9

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