A structured response wrapper for LLMs using Pydantic.
Project description
llmschema
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:
- Try to extract JSON using regex.
- Log warnings for malformed responses.
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmschema-0.1.1.tar.gz.
File metadata
- Download URL: llmschema-0.1.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea6233f25ba342fea8e15d3e19d5aa933f35f356f1af281310158e4cf65d985c
|
|
| MD5 |
2776ae1c64f83595bd420d4bc9ab4701
|
|
| BLAKE2b-256 |
9935ef879f6e9290400d746e94b3d05601bb11a1fe8fbbe7fe3b17bded8bde91
|
File details
Details for the file llmschema-0.1.1-py3-none-any.whl.
File metadata
- Download URL: llmschema-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbbcca8df6a6726533e7f92347b96b373dbea57abbaa1eab827e1e0e3405878a
|
|
| MD5 |
9634026044b3ab3d9bbc95f858a134cc
|
|
| BLAKE2b-256 |
1772569a477c570e57c75816dbfb179512b516440e8682a3d63e104725b18e08
|