Skip to main content

Pydantic v2 models for chat API, automatically generated from JSON schemas

Project description

Chat Schemas - Python

Pydantic v2 models for chat API, automatically generated from JSON schemas using json-schema-to-pydantic.

Installation

From PyPI (when published)

pip install carv-chat-schemas

From GitHub (before publishing)

pip install git+https://github.com/carv-app/carv-recruiter-schema.git#main

Local development

pip install -e /path/to/carv-recruiter-schema/python

Usage

Basic Usage

from carv_chat_schemas import MessageChunk

# Validate data at runtime
data = { /* ... */ }
try:
    message = MessageChunk.model_validate(data)
    print(message.content)
except ValidationError as e:
    print(f"Validation error: {e}")

Stream Events with Discriminated Union

from carv_chat_schemas import (
    StreamEvent,
    parse_stream_event,
    is_message_chunk,
    is_thinking_chunk,
)

# Parse a stream event
event = {
    'type': 'message',
    'content': 'Hello',
    'sequence': 1,
    'message_id': 'msg-123',
}

stream_event = parse_stream_event(event)

# Type narrowing works with discriminated union
if stream_event.type == 'message':
    print(stream_event.content)  # Python knows this is MessageChunk

# Or use type guards
if is_message_chunk(event):
    print(event['content'])

Available Types

All types are exported from the main package:

  • Stream Events: MessageChunk, ThinkingChunk, ToolCallChunk, ErrorChunk, SuggestionChunk
  • Chat Types: ChatResponse, ChatListResponse, CreateChatRequest, UpdateChatRequest, ListChatsRequest
  • Message Types: MessageResponse, SendMessageRequest
  • Pagination: PaginationParams, PaginationResponse, PaginationResponseChatResponse
  • Utility Types: Timestamp, BaseChatEvent, ErrorResponse

Each type is a Pydantic v2 model with full validation support.

Development

Regenerating Models

To regenerate Python models from JSON schemas:

bash scripts/generate-python.sh

Building

cd python
pip install build
python -m build

Testing

cd python
pip install pytest
pytest tests/ -v

Generated Files

All Python files in src/ are generated from JSON schemas in ../schemas/. Do not edit these files directly - regenerate them from the source schemas instead.

The utils.py file contains helper functions and the discriminated union type for StreamEvent and can be edited manually.

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

carv_chat_schemas-3.1.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

carv_chat_schemas-3.1.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file carv_chat_schemas-3.1.1.tar.gz.

File metadata

  • Download URL: carv_chat_schemas-3.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for carv_chat_schemas-3.1.1.tar.gz
Algorithm Hash digest
SHA256 8add1967d0a9c10452fa962ad8dd52829d062a9f59d15f08466d6d4c2dc1c624
MD5 bde45a4f7c5a33a53c2706dcf43a8133
BLAKE2b-256 fddf99646153de702fb9fdca5a7377678c67371ccb8897c5dd055ef67fd0490f

See more details on using hashes here.

File details

Details for the file carv_chat_schemas-3.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for carv_chat_schemas-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 766c1d4c9bfe6c983dc7785d4fc5c71d0e00210f21fff55737f7ce1a855d6b21
MD5 2eefaed9b19532447fecd88490de7961
BLAKE2b-256 3d3aca4dfdd78603c7476dcae7183335da88a53a03426426dcd4d7909b12ece7

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