feat: add Anthropic Messages API provider with core structured output infrastructure#74
Conversation
Add standalone provider package for Anthropic Messages API with mixin
pattern for capability-agnostic reuse.
## Client (AnthropicMessagesClient mixin)
- HTTP POST/streaming to /v1/messages endpoint
- Usage parsing: input_tokens, output_tokens, cached_tokens, total_tokens
- Content array extraction with finish reason (stop_reason)
- Beta header injection system for feature flags
- Default max_tokens=1024 (Anthropic requires this field)
## Parameters
- TemperatureMapper: temperature float [0.0-1.0]
- TopPMapper: nucleus sampling top_p
- TopKMapper: top-k sampling
- MaxTokensMapper: max output tokens
- StopSequencesMapper: custom stop sequences
- ThinkingMapper: extended thinking ("auto" or budget_tokens int)
- OutputSchemaMapper: native structured outputs via output_format
- Supports single BaseModel and list[BaseModel]
- Uses StrictJsonSchemaGenerator for schema generation
- Auto-injects structured-outputs beta header
## Streaming (AnthropicMessagesStream mixin)
- SSE event parsing: content_block_delta, message_delta, message_stop
- Text delta extraction from content_block_delta
- Stop reason extraction from message_delta
- Usage tracking in final message events
## Config
- API version: 2023-06-01
- Endpoints: /v1/messages, /v1/messages/count_tokens
- Beta features: prompt-caching, computer-use, pdfs, token-counting,
max-tokens-sonnet-3.5, structured-outputs
Add core modules required by Anthropic provider package: ## structured_outputs.py - StrictJsonSchemaGenerator: adds additionalProperties:false (OpenAI, Anthropic, xAI) - RefResolvingJsonSchemaGenerator: resolves $ref inline (Cohere) - StrictRefResolvingJsonSchemaGenerator: combines both (Mistral) ## types.py - JsonValue: recursive JSON type alias - StructuredOutput: union of str | JsonValue | BaseModel | list[BaseModel] ## core.py - UsageField: standard field names for usage mapping - INPUT_TOKENS, OUTPUT_TOKENS, TOTAL_TOKENS - CACHED_TOKENS, REASONING_TOKENS, BILLED_TOKENS - NUM_IMAGES, BILLED_UNITS, INPUT_MP, OUTPUT_MP - CACHE_CREATION_INPUT_TOKENS, CACHE_READ_INPUT_TOKENS ## io.py - FinishReason: add reason field (str | None)
Tests cover: - StrictJsonSchemaGenerator: additionalProperties injection - RefResolvingJsonSchemaGenerator: $ref inline resolution - StrictRefResolvingJsonSchemaGenerator: combined transformations 14 test cases covering: - Simple object schemas - Nested model hierarchies - Array item schemas - Non-dict input handling - $defs removal after resolution
Code Review: Anthropic Messages API Provider with Structured Output InfrastructureOverall AssessmentThis is a well-architected PR that introduces foundational infrastructure for Anthropic's Messages API and reusable structured output generators. The code demonstrates strong software engineering practices with excellent documentation, comprehensive test coverage, and thoughtful abstraction design. However, there are a few areas that require attention before merging. Critical Issues1. Incorrect Beta Header Version
|
Summary
This PR introduces the Anthropic Messages API provider package along with foundational structured output infrastructure in the core library, enabling type-safe JSON schema generation reusable across all providers and capabilities.
Anthropic Messages API Provider (
packages/providers/anthropic)A capability-agnostic mixin architecture for the Anthropic Messages API:
Client (
AnthropicMessagesClient)/v1/messageswith full response parsingAsyncIterator[Chunk]with SSE event parsingUsageFieldstandard fieldsParameter Mappers (7 total)
TemperatureMappertemperature(0.0-2.0)TopPMappertop_pnucleus samplingTopKMappertop_ktoken selectionMaxTokensMappermax_tokenswith model-specific defaultsStopSequencesMapperstop_sequencesarrayThinkingMapperthinking_budget→ injectsextended-thinkingbetaOutputSchemaMapperoutput-128k-2025-01-24betaStreaming (
AnthropicMessagesStream)content_block_delta,message_delta,message_stopConfiguration
2023-06-01prompt-caching-2024-07-31,output-128k-2025-01-24,interleaved-thinking-2025-05-14,extended-thinking-2025-05-14Core Structured Output Infrastructure (
src/celeste/)JSON Schema Generators - Reusable across all providers:
StrictJsonSchemaGeneratoradditionalProperties: falserecursivelyRefResolvingJsonSchemaGenerator$refreferences (no$defssupport)StrictRefResolvingJsonSchemaGeneratorType Definitions (
types.py)UsageField Enum - 12 standard field names for cross-provider usage mapping:
INPUT_TOKENS,OUTPUT_TOKENS,TOTAL_TOKENSCACHED_TOKENS,REASONING_TOKENS,BILLED_TOKENSCACHE_CREATION_INPUT_TOKENS,CACHE_READ_INPUT_TOKENSNUM_IMAGES,BILLED_UNITS,INPUT_MP,OUTPUT_MPFinishReason Enhancement - Added
reason: str | Nonefield for capability-specific finish reasonsTest Coverage
14 comprehensive tests for structured output generators covering:
additionalPropertiesinjection$refresolution and$defsremovalTest Plan