Architecture¶
Tessaract has three layers:
your agent code
│ canonical types (UserMessage, FunctionTool, ReasoningOptions, …)
▼
┌──────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ Tessaract │ ──▶ │ Adapter │ ──▶ │ Provider │ ──▶ vendor SDK / API
│ (client.py) │ │ (translation) │ │ (SDK client) │
└──────────────┘ └──────────────────┘ └──────────────────┘
▲ │
│ Response / StreamEventUnion (canonical, with .raw)
└───────────────────────┘
Providers¶
A provider (providers/) holds credentials and the vendor SDK client. OpenAIProvider builds openai.OpenAI(...) from its typed fields plus provider_args, falls back to OPENAI_API_KEY, and imports the SDK lazily. AnthropicProvider does the same with anthropic.Anthropic(...) and ANTHROPIC_API_KEY, and OpenRouterProvider with openrouter.OpenRouter(...) and OPENROUTER_API_KEY, so Tessaract depends only on pydantic unless you install an extra.
Adapters¶
An adapter (adapters/) translates in both directions between canonical types and one vendor's API. The Adapter base class defines the translation hooks:
| Method | Direction | Purpose |
|---|---|---|
map_input_message(UserMessage \| SystemPrompt) |
canonical → native | User turns and system prompts |
map_assistant_message(AssistantMessage) |
canonical → native | Earlier assistant text |
map_output_item(OutputItem) |
canonical → native | Replays earlier reasoning, function call and provider items. The default returns item.raw |
map_tool_result(FunctionToolResult) |
canonical → native | Tool outputs |
map_function_schema(list[FunctionTool]) |
canonical → native | Tool definitions |
map_reasoning_params(ReasoningOptions) |
canonical → native | Reasoning configuration |
map_reasoning(...) |
canonical → native | Reserved |
OpenAIAdapter, AnthropicAdapter and OpenRouterAdapter also implement:
_normalize_output_item/_normalize_output, which turn native output items into canonical ones_normalize_stream_event, which turns native stream events into canonical ones_build_request_kwargs, which merges the canonical parameters withprovider_options(canonical wins)generate_sync(request) -> Responsegenerate_stream(request) -> Iterator[StreamEventUnion]
AnthropicAdapter._build_request_kwargs also reshapes the flat list of mapped items into what the Messages API expects. System items move to the top-level system parameter, after request.instructions. Neighbouring items with the same role are merged into one turn, so a response's thinking, text and tool_use blocks become one assistant turn, and the tool_results that follow become one user turn with the results first. When max_tokens isn't set it sends DEFAULT_ANTHROPIC_MAX_TOKENS (16,000). Requests that use beta-only thinking features go to client.beta.messages with the beta header added.
OpenRouterAdapter uses OpenRouter's Responses API through the OpenRouter SDK, independently of OpenAIAdapter. OpenRouter stores nothing between requests, so it always asks for reasoning.encrypted_content and the history carries the reasoning. The SDK only accepts plain dicts as input, so output items are dumped with model_dump(by_alias=True) before they are replayed. Server tools can come back as OpenRouter's own items (openrouter:web_search, openrouter:shell) or the upstream provider's, and both normalize to the same output items.
The request lifecycle¶
Tessaract.send()splits"oai/gpt-…"into the prefix and the model name, and looks up the provider and adapter._build_request_modelconverts eachinputitem:str→UserMessage→adapter.map_input_messageInputType→item.raw(adapter), which calls the matchingmap_*hookAssistantMessage→adapter.map_assistant_message- other output items →
adapter.map_output_item, which replays the stored.rawnative object - A canonical
Requestgoes toadapter.generate_sync, or toadapter.generate_streamwhenstream=True. - The adapter calls the SDK, then normalizes the result into a
Responseor a stream of events.
Design principles¶
- Canonical first, native always available. Every canonical object keeps the native payload (
raw,raw_response,raw_event), so nothing is lost and you can always go down to the SDK. - Lossless history. Output items replay from
.raw, so provider-specific state such as reasoning IDs and encrypted content survives across turns. - Unknowns pass through. Output items that Tessaract can't model become
ProviderOutputItem, and stream events it can't model becomeCustomProviderEvent. Neither is dropped. - Canonical parameters win.
request_optionsandprovider_optionscan add native parameters, but they can't override the canonical ones.
Adding a provider¶
- Subclass
Providerand create the SDK client in__post_init__fromself._client_kwargs(). Import the SDK lazily and raise a helpfulImportError. - Subclass
Adapterand implement themap_*hooks, output normalization, stream normalization,generate_syncandgenerate_stream. - Register the pair in
Tessaract.register_adapter(). - Add an optional dependency group to
pyproject.toml. - Export the provider from
tessaract/__init__.py.