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)
Metadata
Release files for cforma 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cforma-0.1.2.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cforma-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.3 kB
Release files / cforma-0.1.2.tar.gz
| Download URL | cforma-0.1.2.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
99e902ca5432a396b793ea6e9499e5a4d4d6c98c4f0d7bccced8dd6ebd4bc017
|
|
BLAKE2b-256 checksum How to use checksums |
5bf3c487d3b6378b856edbce81055bcf98d5420d8cc0911f632539be55bdd93b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / cforma-0.1.2-py3-none-any.whl
| Download URL | cforma-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b284e11b2dc98d462bcfc1607063c1f70bd17cc30f105622a37e380a6add6b82
|
|
BLAKE2b-256 checksum How to use checksums |
35662480dc7706dc77f2f219f9adc7f10e0152d737261ce97093a63af416bdec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|