Skip to main content

A Python library for typed prompts

Project description

schema-cat

A Python library for creating typed prompts for Large Language Models (LLMs). Schema-cat allows you to define the structure of LLM responses using Pydantic models, making it easy to get structured, typed data from LLM APIs.

Published by The Famous Cat.

Features

  • Define response structures using Pydantic models
  • Automatically convert Pydantic models to XML schemas
  • Parse LLM responses back into Pydantic models
  • Support for multiple LLM providers:
    • OpenAI
    • Anthropic
    • OpenRouter

Installation

pip install schema-cat

Usage

Basic Usage

from pydantic import BaseModel
from schema_cat import prompt_with_schema, Provider
import asyncio

# Define your response structure
class UserInfo(BaseModel):
    name: str
    age: int
    is_student: bool

# Create a prompt
prompt = "Extract information about John Doe, who is 25 years old and not a student."

# Get a structured response
async def main():
    result = await prompt_with_schema(
        prompt=prompt,
        schema=UserInfo,
        model="gpt-4-turbo",  # Use an appropriate model
        provider=Provider.OPENAI
    )

    print(f"Name: {result.name}")
    print(f"Age: {result.age}")
    print(f"Is student: {result.is_student}")

asyncio.run(main())

Using Different Providers

# OpenAI
result = await prompt_with_schema(prompt, UserInfo, "gpt-4-turbo", Provider.OPENAI)

# Anthropic
result = await prompt_with_schema(prompt, UserInfo, "claude-3-haiku-20240307", Provider.ANTHROPIC)

# OpenRouter
result = await prompt_with_schema(prompt, UserInfo, "anthropic/claude-3-opus-20240229", Provider.OPENROUTER)

Working with Complex Schemas

from pydantic import BaseModel
from typing import List
from schema_cat import prompt_with_schema, Provider

class Address(BaseModel):
    street: str
    city: str
    zip_code: str

class Person(BaseModel):
    name: str
    age: int
    addresses: List[Address]

prompt = """
Extract information about Jane Smith, who is 30 years old.
She has two addresses:
1. 123 Main St, New York, 10001
2. 456 Park Ave, Boston, 02108
"""

async def main():
    result = await prompt_with_schema(prompt, Person, "gpt-4-turbo", Provider.OPENAI)
    print(f"Name: {result.name}")
    print(f"Age: {result.age}")
    print(f"Addresses:")
    for addr in result.addresses:
        print(f"  - {addr.street}, {addr.city}, {addr.zip_code}")

asyncio.run(main())

API Reference

prompt_with_schema(prompt: str, schema: Type[T], model: str, provider: Provider) -> T

Makes a request to an LLM provider with a prompt and schema, returning a structured response.

  • prompt: The prompt to send to the LLM
  • schema: A Pydantic model class defining the expected response structure
  • model: The LLM model to use (e.g., "gpt-4-turbo", "claude-3-haiku")
  • provider: The LLM provider to use (Provider.OPENAI, Provider.ANTHROPIC, or Provider.OPENROUTER)

schema_to_xml(schema: Type[BaseModel]) -> ElementTree.XML

Converts a Pydantic model class to an XML representation.

xml_to_base_model(xml_tree: ElementTree.XML, schema: Type[T]) -> T

Converts an XML element to a Pydantic model instance.

xml_to_string(xml_tree: ElementTree.XML) -> str

Converts an XML element to a pretty-printed string.

Environment Variables

The library uses the following environment variables:

  • OPENAI_API_KEY: Required for OpenAI provider
  • OPENAI_BASE_URL: Optional, defaults to "https://api.openai.com/v1"
  • ANTHROPIC_API_KEY: Required for Anthropic provider
  • OPENROUTER_API_KEY: Required for OpenRouter provider
  • OPENROUTER_BASE_URL: Optional, defaults to "https://openrouter.ai/api/v1"
  • OPENROUTER_HTTP_REFERER: Optional, defaults to "https://www.thefamouscat.com"
  • OPENROUTER_X_TITLE: Optional, defaults to "SchemaCat"

Development

Install dependencies with Poetry:

poetry install

Running Tests

pytest

For end-to-end tests that make actual API calls:

pytest -m slow

Publishing

To publish to PyPI:

poetry build
poetry publish

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

schema_cat-0.1.7.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

schema_cat-0.1.7-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file schema_cat-0.1.7.tar.gz.

File metadata

  • Download URL: schema_cat-0.1.7.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.10 Darwin/24.4.0

File hashes

Hashes for schema_cat-0.1.7.tar.gz
Algorithm Hash digest
SHA256 7519662205366adc41f0b1f01c71486b8c492f03361304fcbfa66d742d413465
MD5 f911acbf6351ebf22798ff20630f8f83
BLAKE2b-256 ff5ee3f252c6aef39cf15493f38adc1d2a350abb2ccff312353c4d89b1f055d9

See more details on using hashes here.

File details

Details for the file schema_cat-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: schema_cat-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.10 Darwin/24.4.0

File hashes

Hashes for schema_cat-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d200b7892ab831947fb318ad005fe12e9deb174556ba3b7e3108aa0760bf5f2a
MD5 9a0cf486a1c6a52f4094d06f34e7ab88
BLAKE2b-256 9425f0da0c09fb1b1c9a4ecfebca2a8f3b686a92df501d206af01a46ac4e49c0

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