Skip to main content

A structured response wrapper for LLMs using Pydantic.

Project description

llmschema

PyPI

llmschema is a Python library that ensures structured and validated responses from LLMs (Large Language Models) like Ollama, OpenAI, and Gemini by enforcing user-defined Pydantic schemas. It abstracts model-specific quirks and guarantees responses in a safe, predictable, and JSON-compliant format.

🚀 Features

Enforces Pydantic schema on LLM responses
Works with multiple LLM providers (Ollama, OpenAI, Gemini, etc.)
Handles malformed JSON responses gracefully
Easy integration into existing applications
Modular & scalable design


📦 Installation

Install llmschema via pip:

pip install llmschema

🛠 Usage

1️⃣ Define a Schema

from pydantic import BaseModel
from llmschema import SchemaManager, generate_response

class MyResponseSchema(BaseModel):
    text: str
    confidence: float

SchemaManager.set_schema(MyResponseSchema)

2️⃣ Generate a Response from an LLM

response = generate_response("mistral", "Summarize the latest AI news")
print(response)  # Output will follow MyResponseSchema format

3️⃣ Handling Errors

from llmschema import LLMValidationError

try:
    response = generate_response("gemini", "Give me a JSON response")
except LLMValidationError as e:
    print("Invalid response:", e)

⚙️ Supported LLMs

llmschema is designed to work with different LLM providers:

  • Ollama (Mistral, Llama, etc.)
  • OpenAI (GPT models)
  • Gemini (Google's LLM)

More integrations coming soon!


✅ Handling Non-JSON Responses

If an LLM outputs invalid JSON, llmschema will:

  1. Try to extract JSON using regex.
  2. Log warnings for malformed responses.
  3. Raise an error if parsing fails completely.

🧪 Running Tests

To test the library locally:

pytest tests/

📜 License

This project is licensed under the MIT License.


🤝 Contributing

Contributions are welcome! Feel free to submit issues and PRs on GitHub.

GitHub Repo: https://github.com/yourusername/llmschema

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

llmschema-0.2.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

llmschema-0.2.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file llmschema-0.2.1.tar.gz.

File metadata

  • Download URL: llmschema-0.2.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for llmschema-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0f4b3477d04f2fbbfc68a3c3c9a4875f920b80bcf3ffe435e596550a51bc1c9b
MD5 61b358904d13ad56ec8c0589a63a72e8
BLAKE2b-256 0874f0d896b933a8a3d8955a069f1610eded71d4e72cfa7cab0930914b8c9b0f

See more details on using hashes here.

File details

Details for the file llmschema-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: llmschema-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for llmschema-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 376e409c88cb62ee1852da8cbf08ac4919f5377086cd72b33be3a5e41d8d54d3
MD5 16b575ef8c36e6059f993d7a59e9a951
BLAKE2b-256 645c947996d0644b93db70e21e0c74726b646cc556caaf58b7c9f2af9ba6e59b

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