Skip to main content
The OpenAICompletionsClient class provides a wrapper around the OpenAI Completions API (legacy text completion endpoint) using the AsyncOpenAI client.

Overview

This client implements the Client interface for OpenAI’s legacy Completions API, which is used for text completion models. It handles:
  • Message conversion to plain text format (Completions API only accepts text prompts)
  • Text-only content validation (rejects images and other multimodal content)
  • Token usage and logprobs parsing
  • Context length error handling
The Completions API does not support tools, function calling, or multimodal content. Use OpenAIChatCompletionsClient for these features.

Type Aliases

Class Definition

Generic type parameters:
  • ClientT: AsyncOpenAI - The OpenAI async client
  • MessagesT: OpenAITextMessages - Plain text string (concatenated messages)
  • ResponseT: OpenAITextResponse - OpenAI Completion object
  • ToolT: None - Tools are not supported

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 plain text format for the Completions API.
Messages
required
List of Verifiers message objects. All messages will be concatenated with double newlines.
Returns: Tuple of (text_prompt, extra_kwargs). The text prompt is a string with all message contents joined by "\n\n". The extra_kwargs dict is currently empty. Raises:
  • ValueError if any message contains non-text content (e.g., images)

to_native_tool

Not supported for Completions API.
Tool
required
A Verifiers tool definition.
Raises: ValueError with message “Tools are not supported for Completions API”

get_native_response

Calls the OpenAI Completions API and returns the native response.
OpenAITextMessages
required
Plain text prompt string.
str
required
OpenAI model identifier (e.g., "gpt-3.5-turbo-instruct").
SamplingArgs
required
Sampling parameters. None values are filtered out before sending to API.
list[None] | None
default:"None"
Must be None or empty. Tools are not supported.
Returns: OpenAI Completion object. Raises:
  • ValueError if tools are provided
  • OverlongPromptError if the prompt exceeds the model’s context length

raise_from_native_response

Validates the OpenAI response and raises errors if invalid.
OpenAITextResponse
required
The OpenAI Completion response.
Raises:
  • EmptyModelResponseError if response is None, has no choices, or the text is empty
  • InvalidModelResponseError if the response has more than 1 choice

from_native_response

Converts an OpenAI Completion to a Verifiers Response.
OpenAITextResponse
required
The OpenAI Completion response.
Returns: Verifiers Response object with:
  • id: Response ID from OpenAI
  • created: Timestamp from OpenAI
  • model: Model name from response
  • usage: Token counts (prompt_tokens, completion_tokens, total_tokens, reasoning_tokens=0)
  • message: Response message with text content and metadata
Parsed fields:
  • Content: Text from response.choices[0].text
  • Finish reason: Mapped from OpenAI values ("stop""stop", "length""length", others → None)
  • Is truncated: True if finish_reason is "length"
  • Tokens: If available (vLLM with return_tokens=true), includes prompt_token_ids, token_ids, logprobs
  • Reasoning content: Always None (not supported by Completions API)
  • Tool calls: Always None (not supported by Completions API)

Usage Example

Limitations

No Tool Support

No Multimodal Content

Token Details

The client attempts to parse token-level information from the response:

Error Handling

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