OpenAIChatCompletionsClient class provides a wrapper around the OpenAI Chat Completions API using the AsyncOpenAI client.
Overview
This client implements theClient interface for OpenAI’s Chat Completions API, handling:
- Message format conversion between Verifiers and OpenAI formats
- Tool/function calling support
- Reasoning content extraction (for models with reasoning capabilities)
- Audio input handling with automatic modality detection
- Token usage and logprobs parsing
- Context length error handling
Type Aliases
Class Definition
- ClientT:
AsyncOpenAI- The OpenAI async client - MessagesT:
OpenAIChatMessages- List of OpenAI message parameters - ResponseT:
OpenAIChatResponse- OpenAI ChatCompletion object - ToolT:
OpenAITool- OpenAI tool parameter type
Constructor
Either a pre-configured
AsyncOpenAI client or a ClientConfig to create one.Example
Methods
setup_client
AsyncOpenAI client from a ClientConfig.
Configuration with API key, base URL, and other settings.
AsyncOpenAI instance.
close
AsyncOpenAI client connection.
to_native_prompt
List of Verifiers message objects (
SystemMessage, UserMessage, AssistantMessage, ToolMessage, or TextMessage).(openai_messages, extra_kwargs). The extra_kwargs dict is currently empty.
Supported message types:
SystemMessage→ChatCompletionSystemMessageParamUserMessage→ChatCompletionUserMessageParamAssistantMessage→ChatCompletionAssistantMessageParam(with tool calls if present)ToolMessage→ChatCompletionToolMessageParamTextMessage→ChatCompletionUserMessageParam
to_native_tool
Tool to OpenAI’s tool parameter format.
Verifiers tool definition with name, description, parameters, and optional strict mode.
ChatCompletionToolParam with type=“function”.
get_native_response
List of OpenAI message parameters.
OpenAI model identifier (e.g.,
"gpt-4", "gpt-4o-mini", "o1-preview").Sampling parameters.
max_tokens is automatically renamed to max_completion_tokens for the API.Optional list of tools in OpenAI format.
ChatCompletion object.
Raises: OverlongPromptError if the prompt exceeds the model’s context length.
Special handling:
- Audio inputs: Automatically sets
modalities=["text"]unless explicitly specified - Sampling args: Converts
max_tokenstomax_completion_tokens, filters out None values
raise_from_native_response
The OpenAI ChatCompletion response.
EmptyModelResponseErrorif response is None, has no choices, or the message has no content/tool calls/reasoningInvalidModelResponseErrorif the response has more than 1 choice
from_native_response
ChatCompletion to a Verifiers Response.
The OpenAI ChatCompletion response.
Response object with:
id: Response ID from OpenAIcreated: Timestamp from OpenAImodel: Model name from responseusage: Token counts (prompt, completion, total)message: Response message with content, tool calls, finish reason, and optional tokens/logprobs
- Content: Text content from the message
- Reasoning content: Extracted from provider-specific fields (
reasoning,reasoning_content, orreasoning_details) - Tool calls: Converted from OpenAI format to Verifiers
ToolCallobjects - Finish reason: Mapped from OpenAI values (
"stop","length","tool_calls", orNone) - Tokens: If available, includes prompt IDs, completion IDs, masks, logprobs, and routed experts data
Usage Example
Reasoning Content Support
The client automatically extracts reasoning content from models that support it. It checks the following fields in order:reasoning(vLLM, Together AI, OpenRouter)reasoning_content(DeepSeek, Qwen/DashScope, SGLang, Fireworks AI, Kimi/Moonshot)reasoning_details(OpenRouter, MiniMax)
response.message.reasoning_content.
Error Handling
Overlong Prompt Errors
The@handle_openai_overlong_prompt decorator catches BadRequestError and converts context length errors to OverlongPromptError. It detects phrases like:
- “this model’s maximum context length is”
- “is longer than the model’s context length”
- “prompt_too_long”
- “context length”
See Also
- Client - Base client class
- AnthropicMessagesClient - Anthropic implementation
- ClientConfig - Configuration type
- Response - Response type
- Tool - Tool definition type