AnthropicUtils
parse_message_param
to be parsed. Can contain strings, dicts, or MessageParam objects.
override_roleOptional[str] - Optional role to override the message role. If provided, all messages will use this role instead of their original role.
with role and content extracted and standardized.
Notes:
- String messages are treated as user messages by default
- Dict messages should have ‘role’ and ‘content’ keys
- Content blocks are flattened into text content
- Complex content structures are converted to string representation
get_model_params
model parameters such as temperature, top_p, top_k, system, etc.
Returns:
Dict[str, Any]: Dictionary containing normalized model parameters
with non-None values only. Common parameters are extracted
explicitly while additional parameters are included as-is.
Notes:
- Only non-None parameters are included in the result
- System, metadata, temperature, top_p, and top_k are handled explicitly
- Additional parameters from kwargs are included if they have values
parse_message_stream
Anthropic’s streaming API response.
Returns:
Dict[str, Any]: Standardized response dictionary with the following structure:
- id: Unique identifier for the response
- created: Unix timestamp of creation
- choices: List with single choice containing message and finish_reason
- usage: Token usage statistics (prompt, completion, total)
ValueError- If the stream list is empty.
- Text content is extracted from content_block_delta events
- Token usage is extracted from message_start events
- The response format mimics OpenAI’s API for compatibility
- Finish reason is set to “stop” by default (Anthropic doesn’t provide this directly)
parse_message
with content, usage statistics, and metadata.
Returns:
Dict[str, Any]: Standardized response dictionary with the following structure:
- id: Message ID from Anthropic
- created: Unix timestamp of parsing time
- choices: List with single choice containing message and finish_reason
- usage: Token usage statistics (input, output, total tokens)
- Content blocks are flattened into a single text string
- Both structured content blocks and dict-based content are supported
- Token usage is extracted from the message’s usage attribute
- Stop reason is mapped from Anthropic’s stop_reason or defaults to “stop”
- The response format mimics OpenAI’s API for cross-provider compatibility