A utility to format Pydantic models into clean JSON Schemas for LLMs.
Project description
cforma
A utility to format Pydantic models into clean, LLM-ready JSON Schemas.
Description
cforma introspects Pydantic models, resolves all nested references, and cleans the resulting schema to make it minimal and efficient for use with Large Language Models that support structured JSON output (Tested with OpenRouter).
Installation
pip install cforma
Usage
Here is how to convert your Pydantic models into a schema and use it in an API call.
1. Define your Pydantic Models
You can define complex, nested models. cforma will handle them automatically.
from pydantic import BaseModel, Field
from typing import List
class Author(BaseModel):
name: str = Field(description="The author's full name.")
is_prolific: bool = Field(description="True if the author has written more than 10 books.")
class Book(BaseModel):
title: str = Field(description="The title of the book.")
published_year: int = Field(description="The year the book was published.")
authors: List[Author] = Field(description="A list of the book's authors.")
2. Generate the Schema
Import StructFormatter and use the ingest method to generate the complete schema required by the LLM.
from cforma import StructFormatter
llm_schema = StructFormatter.ingest(
schemaName="BookSchema",
schemaDescription="A schema to extract detailed information about a book and its authors.",
schemaObject=Book
)
3. Use the Schema in an API Call
You can now pass the generated llm_schema directly into the response_format parameter of your LLM API call (e.g., using an OpenAI-compatible client with OpenRouter).
# This is a hypothetical example using an OpenAI-compatible client
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR_OPENROUTER_KEY",
)
response = client.chat.completions.create(
model="google/gemini-flash-1.5",
messages=[
{"role": "user", "content": "Extract the book details for 'The Hobbit'."},
],
extra_body={
"response_format": llm_schema
}
)
# The response will contain structured JSON matching your Book model
# print(response.choices[0].message.content)
Project details
Release history Release notifications | RSS feed
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 cforma-0.1.1.tar.gz.
File metadata
- Download URL: cforma-0.1.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e73c441a8b6442f7ff699263127e54e9f1344c13577d30e42edaf910de844b3
|
|
| MD5 |
27e4070bc0e06cd3892c821cb0494334
|
|
| BLAKE2b-256 |
4e0fe8de55b20558861c1e77fd95a208ce4445e31bd0935be0b978429ffd9b48
|
File details
Details for the file cforma-0.1.1-py3-none-any.whl.
File metadata
- Download URL: cforma-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
075e32a0758d1027cd1eefe9efc0ccf1cdd8266a8e4bd7b03a2a81ebd1038e92
|
|
| MD5 |
f0a42e739412593cf5ec9c106d9412a3
|
|
| BLAKE2b-256 |
753e04b2bf007fcb63a1c00092735c520e49270e09c5468492bce7f2d0604e83
|