yera.models.interfaces.llms.anthropic

Module containing the interface to Anthropic LLMs.

Symbols

class AnthropicLLM — Interface to Anthropic LLM.

AnthropicLLM

Interface to Anthropic LLM.

This class provides a wrapper around the Anthropic API client, handling configuration, model selection, and streaming interactions with Claude models. It supports both standard chat completions and structured output generation for models that support it (Claude 4.5+).

Attributes

model_id
type: str

The identifier of the Claude model to use.

connection
type: AnthropicConnection

Connection configuration for the Anthropic API.

struct_gen
type: bool

Whether the selected model supports structured outputs.

client
type: Anthropic

Lazy-initialised Anthropic API client instance.

Methods

start — Initialize the Anthropic API client.
stop — Shut down and clear the Anthropic API client.
chat — Stream a chat completion response with extended thinking support.
make_struct — Stream a structured output response conforming to a provided schema.

AnthropicLLM.start

start() → None

Initialize the Anthropic API client.

Creates and stores an Anthropic client instance using the configured connection settings. This method must be called before making any API requests via the client property.

AnthropicLLM.stop

stop() → None

Shut down and clear the Anthropic API client.

Releases the Anthropic client instance by setting it to None. After calling this method, start() must be called again before further API requests can be made.

AnthropicLLM.chat

chat(
    messages: list[Message],
    max_tokens: int = 4096,
    **anthropic_kw,
) → Iterator[str]

Stream a chat completion response with extended thinking support.

Sends a conversation to the Anthropic API and streams the response as a series of text tokens. Extended thinking is enabled by default with a 1024 token budget for internal reasoning.

System messages are extracted from the message list and combined into a single system parameter, as the Anthropic API only accepts a single system prompt at the start of a conversation.

Parameters

messages
type: list[Message]

List of Message objects representing the conversation history. Messages with role="system" are extracted and combined into the system parameter. Other messages are passed as conversation turns.

max_tokens
type: int = 4096

Maximum number of tokens to generate in the response. Defaults to 4096.

**anthropic_kw
type: str | int | float | bool

Additional keyword arguments passed to the Anthropic API (e.g., temperature, top_p, stop_sequences).

AnthropicLLM.make_struct

make_struct(
    messages: list[Message],
    max_tokens: int = 4096,
    **anthropic_kw,
) → Iterator[str]

Stream a structured output response conforming to a provided schema.

Generates a response that conforms to the structure defined by the provided Pydantic model class. For models supporting native structured outputs (Claude 4.5+), uses the native API. For older models, falls back to tool use with the schema as the tool's input_schema.

System messages are extracted from the message list and combined into a single system parameter, as the Anthropic API only accepts a single system prompt at the start of a conversation.

Parameters

messages
type: list[Message]

List of Message objects representing the conversation history. Messages with role="system" are extracted and combined into the system parameter. Other messages are passed as conversation turns.

cls
type: type[TStruct]

A Pydantic model class defining the desired output structure. The model will be transformed into a JSON schema for the API.

max_tokens
type: int = 4096

Maximum number of tokens to generate in the response. Defaults to 4096.

**anthropic_kw
type: str | float | int | bool

Additional keyword arguments passed to the Anthropic API (e.g., temperature, top_p).

Raises

ValueError

If the model does not support structured outputs and the tool use fallback was not invoked.