Skip to main content
The OpenAIChatCompletionsClient class provides a wrapper around the OpenAI Chat Completions API using the AsyncOpenAI client.

Overview

This client implements the Client 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

Generic type parameters:
  • 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

AsyncOpenAI | ClientConfig
required
Either a pre-configured AsyncOpenAI client or a ClientConfig to create one.

Example

Methods

setup_client

Creates an AsyncOpenAI client from a ClientConfig.
ClientConfig
required
Configuration with API key, base URL, and other settings.
Returns: Configured AsyncOpenAI instance.

close

Closes the underlying AsyncOpenAI client connection.

to_native_prompt

Converts Verifiers messages to OpenAI’s chat completion format.
Messages
required
List of Verifiers message objects (SystemMessage, UserMessage, AssistantMessage, ToolMessage, or TextMessage).
Returns: Tuple of (openai_messages, extra_kwargs). The extra_kwargs dict is currently empty. Supported message types:
  • SystemMessageChatCompletionSystemMessageParam
  • UserMessageChatCompletionUserMessageParam
  • AssistantMessageChatCompletionAssistantMessageParam (with tool calls if present)
  • ToolMessageChatCompletionToolMessageParam
  • TextMessageChatCompletionUserMessageParam

to_native_tool

Converts a Verifiers Tool to OpenAI’s tool parameter format.
Tool
required
Verifiers tool definition with name, description, parameters, and optional strict mode.
Returns: ChatCompletionToolParam with type=“function”.

get_native_response

Calls the OpenAI Chat Completions API and returns the native response.
OpenAIChatMessages
required
List of OpenAI message parameters.
str
required
OpenAI model identifier (e.g., "gpt-4", "gpt-4o-mini", "o1-preview").
SamplingArgs
required
Sampling parameters. max_tokens is automatically renamed to max_completion_tokens for the API.
list[OpenAITool] | None
default:"None"
Optional list of tools in OpenAI format.
Returns: OpenAI 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_tokens to max_completion_tokens, filters out None values

raise_from_native_response

Validates the OpenAI response and raises errors if invalid.
OpenAIChatResponse
required
The OpenAI ChatCompletion response.
Raises:
  • EmptyModelResponseError if response is None, has no choices, or the message has no content/tool calls/reasoning
  • InvalidModelResponseError if the response has more than 1 choice

from_native_response

Converts an OpenAI ChatCompletion to a Verifiers Response.
OpenAIChatResponse
required
The OpenAI ChatCompletion response.
Returns: Verifiers Response object with:
  • id: Response ID from OpenAI
  • created: Timestamp from OpenAI
  • model: Model name from response
  • usage: Token counts (prompt, completion, total)
  • message: Response message with content, tool calls, finish reason, and optional tokens/logprobs
Parsed fields:
  • Content: Text content from the message
  • Reasoning content: Extracted from provider-specific fields (reasoning, reasoning_content, or reasoning_details)
  • Tool calls: Converted from OpenAI format to Verifiers ToolCall objects
  • Finish reason: Mapped from OpenAI values ("stop", "length", "tool_calls", or None)
  • 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:
  1. reasoning (vLLM, Together AI, OpenRouter)
  2. reasoning_content (DeepSeek, Qwen/DashScope, SGLang, Fireworks AI, Kimi/Moonshot)
  3. reasoning_details (OpenRouter, MiniMax)
Reasoning content is available in 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”
Authentication and permission errors are re-raised without wrapping.

See Also