Metadata-Version: 2.4
Name: cforma
Version: 0.1.1
Summary: A utility to format Pydantic models into clean JSON Schemas for LLMs.
Author-email: Grace Peter Mutiibwa <temporarybag.grace@gmail.com>
License: Copyright 2025 GRACE PETER MUTIIBWA
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        
Project-URL: Homepage, https://github.com/GracePeterMutiibwa/cforma
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Utilities
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pydantic
Dynamic: license-file

# 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.

```python
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.

```python
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).

```python
# 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)
```
