Expand description
Universal LLM provider abstraction with API-specific role handling
This module provides a unified interface for different LLM providers (OpenAI, Anthropic, Gemini) while properly handling their specific requirements for message roles and tool calling.
§Message Role Mapping
Different LLM providers have varying support for message roles, especially for tool calling:
§OpenAI API
- Full Support:
system,user,assistant,tool - Tool Messages: Must include
tool_call_idto reference the original tool call - Tool Calls: Only
assistantmessages can containtool_calls
§Anthropic API
- Standard Roles:
user,assistant - System Messages: Can be hoisted to system parameter or treated as user messages
- Tool Responses: Converted to
usermessages (no separate tool role) - Tool Choice: Supports
auto,any,tool,nonemodes
§Gemini API
- Conversation Roles: Only
userandmodel(notassistant) - System Messages: Handled separately as
systemInstructionparameter - Tool Responses: Converted to
usermessages withfunctionResponseformat - Function Calls: Uses
functionCallinmodelmessages
§Best Practices
- Always use
MessageRole::tool_response()constructor for tool responses - Validate messages using
validate_for_provider()before sending - Use appropriate role mapping methods for each provider
- Handle provider-specific constraints (e.g., Gemini’s system instruction requirement)
§Example Usage
ⓘ
use vtcode_llm::provider::{Message, MessageRole};
// Create a proper tool response message
let tool_response = Message::tool_response(
"call_123".to_string(),
"Tool execution completed successfully".to_string()
);
// Validate for specific provider
tool_response.validate_for_provider("openai").unwrap();Structs§
- Anthropic
Request Overrides - Coding
Agent Settings - Settings to refine model behavior for coding agent tasks
- Context
Window Provider - Provider-local context capacity discovered for the selected model. All transport and capability behavior remains delegated to the underlying provider.
- Fallback
Model - Fallback model configuration for Anthropic server-side fallback.
The explicit-list form of
fallbacks, sent with theserver-side-fallback-2026-06-01beta header (the"default"keyword form usesserver-side-fallback-2026-07-01; pairing a header with the other form is rejected). - Function
Call - Function call within a tool call
- Function
Definition - Function definition within a tool
- Grammar
Definition - Grammar definition for GPT-5 context-free grammar (CFG) constraints
- LLMError
Metadata - LLMRequest
- Universal LLM request structure
- LLMResponse
- Universal LLM response structure
- Message
- Universal message structure supporting both text and image content
- Parallel
Tool Config - Configuration for parallel tool use behavior Based on Anthropic’s parallel tool use guidelines
- Prepared
Responses Request - Provider
Capabilities - Cached provider capabilities to reduce repeated trait method calls
- Responses
Compaction Options - Optional overrides for standalone Responses compaction requests.
- Responses
Continuation State - Sampling
Overrides - Per-model sampling parameter overrides resolved from a custom provider
profile. Every field defaults to
None, meaning the agent loop’s global configuration value applies unchanged. - Shell
Tool Definition - Shell tool definition for GPT-5.1 shell tool type Allows controlled command-line interface interactions
- Specific
Function Choice - Specific function choice details
- Specific
Tool Choice - Specific tool choice for forcing a particular function call
- Tool
Call - Universal tool call that matches OpenAI/Anthropic/Gemini specifications
- Tool
Definition - Universal tool definition that matches OpenAI/Anthropic/Gemini specifications Based on official API documentation from Context7
- Tool
Namespace - A namespace grouping for deferred tools. When a tool has a namespace and
defer_loadingis true, only the namespace metadata (name + description) is sent upfront. The full tool definitions are loaded when the model searches for a tool in that namespace. - Usage
Enums§
- Anthropic
Optional String Override - Anthropic
Optional U32Override - Anthropic
Thinking Config - Anthropic thinking configuration for fallback models.
- Anthropic
Thinking Display Override - Anthropic
Thinking Mode Override - Assistant
Phase - Phase metadata for assistant messages in multi-step Responses-style workflows.
- Content
Part - Content type for messages that can include both text and images
- Finish
Reason - Image
Detail - Detail level for image processing (DeepSeek/OpenAI
detailfield). - LLMError
- LLM error types with optional provider metadata
- LLMStream
Event - Message
Clear At - Controls when an Anthropic mid-conversation system message is cleared.
- Message
Content - Message
Role - Normalized
Stream Event - Prompt
Cache Profile - Reasoning
Source - Provider-side classification for streamed reasoning text.
- Tool
Choice - Tool choice configuration that works across different providers Based on OpenAI, Anthropic, and Gemini API specifications Follows Anthropic’s tool use best practices for optimal performance
- Tool
Search Algorithm - Tool search algorithm for Anthropic’s advanced-tool-use beta
Traits§
- LLMProvider
- Universal LLM provider trait
Functions§
- get_
cached_ capabilities - Extract and cache provider capabilities for a given provider and model
- prepare_
openai_ responses_ request - prepare_
responses_ continuation_ request - records_
responses_ continuation_ state - responses_
continuation_ key - supports_
responses_ chaining - uses_
incremental_ responses_ history - Compatibility shim for older callers.