Skip to main content

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

Metadata

Release files for llmschema 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmschema 0.3
File Size Uploaded Provenance Actions
llmschema-0.3.tar.gz 8.6 kB Without provenance Details for llmschema-0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llmschema 0.3
File Interpreter ABI Platform Actions
llmschema-0.3-py3-none-any.whl Python 3 none any Details for llmschema-0.3-py3-none-any.whl

Total release size: 16.9 kB

Release files / llmschema-0.3.tar.gz

Download URL llmschema-0.3.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e01ddbe5078cd1943974a35d38b91032c9a0dddaa9b7e0b18d2f0656f625967b
BLAKE2b-256 checksum
How to use checksums
21246ffcd06b9747351a1bb958c24a883cc13143eec519d2e499b90f2def8b10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / llmschema-0.3-py3-none-any.whl

Download URL llmschema-0.3-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e357d65ad67bb18fff046bea51fa9a154c2480acaffeec5a7095bc23fe0879b2
BLAKE2b-256 checksum
How to use checksums
722da3165d25deefa086d73469a2510101b5b71d12575dac696529d5170afd6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Security for llmschema 0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page