mod response#

module response#

Normalized LLM response types produced by response codecs.

This module defines AnnotatedLlmResponse and its supporting types for structured, API-agnostic access to LLM response data.

Enums

enum ApiSpecificResponse#

API-specific response data that cannot be normalized across providers.

Each variant captures fields unique to a particular LLM API, stored via internal tagging on the "api" key.

OpenAIChat#

OpenAI Chat Completions-specific fields.

logprobs: Option<Json>#

Token-level log probabilities (raw JSON, too complex to normalize).

system_fingerprint: Option<String>#

System fingerprint for reproducibility.

service_tier: Option<String>#

Processing tier used (e.g., “default”).

OpenAIResponses#

OpenAI Responses API-specific fields.

output_items: Option<Vec<Json>>#

Full output items array for direct access.

status: Option<String>#

Response status (e.g., “completed”, “incomplete”).

incomplete_details: Option<Json>#

Details about why the response is incomplete.

AnthropicMessages#

Anthropic Messages API-specific fields.

stop_sequence: Option<String>#

Which stop sequence was matched (if any).

content_blocks: Option<Vec<Json>>#

Full content blocks array for direct access.

Custom#

Custom/unknown API – catch-all for user-implemented codecs.

api_name: String#

API identifier.

data: Json#

Opaque API-specific data.

enum FinishReason#

Normalized reason why the model stopped generating.

Maps from provider-specific stop reasons:

  • Complete: OpenAI Chat "stop", Anthropic "end_turn", Responses "completed"

  • Length: OpenAI Chat "length", Anthropic "max_tokens", Responses incomplete+max_output_tokens

  • ToolUse: OpenAI Chat "tool_calls", Anthropic "tool_use"

  • ContentFilter: OpenAI Chat "content_filter", Responses incomplete+content_filter

  • Unknown: Forward-compatible catch-all for unrecognized reasons

Complete#

Model naturally completed its response.

Length#

Maximum token limit reached.

ToolUse#

Model requested a tool call.

ContentFilter#

Content was filtered by safety systems.

Unknown(String)#

Unknown or forward-compatible reason.

Implementations

impl FinishReason#

Functions

fn is_complete(&self) -> bool#

Returns true if the model naturally completed its response.

Only the FinishReason::Complete variant returns true.

Structs and Unions

struct AnnotatedLlmResponse#

Structured view of an LLM response, produced by a response codec from raw JSON API output.

The extra field captures any top-level keys not modeled by the known fields, ensuring lossless round-trip through serde.

id: Option<String>#

Response ID from the API (e.g., “chatcmpl-abc123”, “resp_abc123”, “msg_abc123”).

model: Option<String>#

The model that actually served the request (may differ from requested model).

message: Option<MessageContent>#

The assistant’s response content, reusing MessageContent from request types.

tool_calls: Option<Vec<ResponseToolCall>>#

Tool calls requested by the model, normalized across APIs.

Uses ResponseToolCall (arguments as Json) NOT the request-side ToolCall (arguments as String).

finish_reason: Option<FinishReason>#

Why generation stopped, normalized across APIs.

usage: Option<Usage>#

Token usage statistics.

api_specific: Option<ApiSpecificResponse>#

API-specific response data that cannot be normalized across providers.

extra: serde_json::Map<String, Json>#

Catch-all for unmodeled top-level fields, ensuring lossless round-trip.

Implementations

impl AnnotatedLlmResponse#

Functions

fn has_tool_calls(&self) -> bool#

Check if the response contains any tool calls.

Returns true if tool_calls is Some with at least one element.

fn response_text(&self) -> Option<&str>#

Extract the text content of the response message.

For MessageContent::Text, returns the string directly. For MessageContent::Parts, returns the text of the first super::request::ContentPart::Text part. Returns None if message is None.

struct ResponseToolCall#

A tool call requested by the model in its response.

Unlike the request-side ToolCall (which stores arguments as a JSON string per OpenAI convention), response tool calls store arguments as parsed Json. Codecs parse OpenAI’s string arguments during decode; Anthropic’s input is already parsed JSON.

id: String#

Unique identifier for this tool call.

name: String#

The function/tool name.

arguments: Json#

The arguments as parsed JSON (not a string).

struct Usage#

Token usage statistics from an LLM API response.

All fields are Option<u64> because not every provider supplies every field. For example, cache token counts are only available from providers that support prompt caching.

prompt_tokens: Option<u64>#

Tokens consumed by the prompt/input.

completion_tokens: Option<u64>#

Tokens generated in the completion/output.

total_tokens: Option<u64>#

Total tokens (prompt + completion).

cache_read_tokens: Option<u64>#

Tokens served from prompt cache (read).

cache_write_tokens: Option<u64>#

Tokens written to prompt cache.