Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cforma-0.1.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

cforma-0.1.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file cforma-0.1.2.tar.gz.

File metadata

  • Download URL: cforma-0.1.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for cforma-0.1.2.tar.gz
Algorithm Hash digest
SHA256 99e902ca5432a396b793ea6e9499e5a4d4d6c98c4f0d7bccced8dd6ebd4bc017
MD5 5a2efc5063c64dc583bd159c0ce711ea
BLAKE2b-256 5bf3c487d3b6378b856edbce81055bcf98d5420d8cc0911f632539be55bdd93b

See more details on using hashes here.

File details

Details for the file cforma-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: cforma-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for cforma-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b284e11b2dc98d462bcfc1607063c1f70bd17cc30f105622a37e380a6add6b82
MD5 fe0d1ca41807717e08a59571fa01fb30
BLAKE2b-256 35662480dc7706dc77f2f219f9adc7f10e0152d737261ce97093a63af416bdec

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