Skip to content

Openai chat completions lm invoker

Defines a module to interact with OpenAI language models using the Chat Completions API.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id)

References

[1] https://platform.openai.com/docs/api-reference/chat

OpenAIChatCompletionsLMInvoker(model_name, api_key=None, base_url=OPENAI_DEFAULT_URL, model_kwargs=None, default_hyperparameters=None, tools=None, response_schema=None, output_analytics=False, retry_config=None, reasoning_effort=None, simplify_events=False)

Bases: BaseLMInvoker

A language model invoker to interact with OpenAI language models using the Chat Completions API.

This class provides support for OpenAI's Chat Completions API schema. Use this class only when you have a specific reason to use the Chat Completions API over the Responses API, as OpenAI recommends using the Responses API whenever possible. The Responses API schema is supported through the OpenAILMInvoker class.

Attributes:

Name Type Description
model_id str

The model ID of the language model.

model_provider str

The provider of the language model.

model_name str

The name of the language model.

client_kwargs dict[str, Any]

The keyword arguments for the OpenAI client.

default_hyperparameters dict[str, Any]

Default hyperparameters for invoking the model.

tools list[Tool]

The list of tools provided to the model to enable tool calling.

response_schema ResponseSchema | None

The schema of the response. If provided, the model will output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.

output_analytics bool

Whether to output the invocation analytics.

retry_config RetryConfig | None

The retry configuration for the language model.

Basic usage

The OpenAIChatCompletionsLMInvoker can be used as follows:

lm_invoker = OpenAIChatCompletionsLMInvoker(model_name="gpt-5-nano")
result = await lm_invoker.invoke("Hi there!")
OpenAI compatible endpoints

The OpenAIChatCompletionsLMInvoker can also be used to interact with endpoints that are compatible with OpenAI's Chat Completions API schema. This includes but are not limited to: 1. DeepInfra (https://deepinfra.com/) 2. DeepSeek (https://deepseek.com/) 3. Groq (https://groq.com/) 4. OpenRouter (https://openrouter.ai/) 5. Text Generation Inference (https://github.com/huggingface/text-generation-inference) 6. Together.ai (https://together.ai/) 7. vLLM (https://vllm.ai/) Please note that the supported features and capabilities may vary between different endpoints and language models. Using features that are not supported by the endpoint will result in an error.

This customization can be done by setting the base_url parameter to the base URL of the endpoint:

lm_invoker = OpenAIChatCompletionsLMInvoker(
    model_name="llama3-8b-8192",
    api_key="<your-api-key>",
    base_url="https://api.groq.com/openai/v1",
)
result = await lm_invoker.invoke("Hi there!")
Input types

The OpenAIChatCompletionsLMInvoker supports the following input types: text, audio, document, and image. Non-text inputs can be passed as an Attachment object with the user role.

Usage example:

text = "What animal is in this image?"
image = Attachment.from_path("path/to/local/image.png")
result = await lm_invoker.invoke([text, image])
Text output

The OpenAIChatCompletionsLMInvoker generates text outputs by default. Text outputs are stored in the outputs attribute of the LMOutput object and can be accessed via the texts (all text outputs) or text (first text output) properties.

Output example:

LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
Structured output

The OpenAIChatCompletionsLMInvoker can be configured to generate structured outputs. This feature can be enabled by providing a schema to the response_schema parameter.

Structured outputs are stored in the outputs attribute of the LMOutput object and can be accessed via the structureds (all structured outputs) or structured (first structured output) properties.

The schema must either be one of the following: 1. A Pydantic BaseModel class The structured output will be a Pydantic model. 2. A JSON schema dictionary JSON dictionary schema must be compatible with Pydantic's JSON schema, especially for complex schemas. Thus, it is recommended to create the JSON schema using Pydantic's model_json_schema method. The structured output will be a dictionary.

Usage example:

class Animal(BaseModel):
    name: str
    color: str

json_schema = Animal.model_json_schema()

lm_invoker = OpenAIChatCompletionsLMInvoker(..., response_schema=Animal)  # Using Pydantic BaseModel class
lm_invoker = OpenAIChatCompletionsLMInvoker(..., response_schema=json_schema)  # Using JSON schema dictionary

Output example:

# Using Pydantic BaseModel class outputs a Pydantic model
LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])

# Using JSON schema dictionary outputs a dictionary
LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])

When structured output is enabled, streaming is disabled.

Tool calling

The OpenAIChatCompletionsLMInvoker can be configured to call tools to perform certain tasks. This feature can be enabled by providing a list of Tool objects to the tools parameter.

Tool calls outputs are stored in the outputs attribute of the LMOutput object and can be accessed via the tool_calls property.

Usage example:

lm_invoker = OpenAIChatCompletionsLMInvoker(..., tools=[tool_1, tool_2])

Output example:

LMOutput(
    outputs=[
        LMOutputItem(type="text", output="I'm using tools..."),
        LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
        LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
    ]
)
Reasoning

The OpenAILMInvoker performs step-by-step reasoning before generating a response when reasoning models are used, such as GPT-5 models and o-series models.

The reasoning effort can be set via the reasoning_effort parameter, which guides the models on the amount of reasoning tokens to generate. Available options include minimal, low, medium, and high.

Some models may also output the reasoning tokens. In this case, the reasoning tokens are stored in the outputs attribute of the LMOutput object and can be accessed via the thinkings property.

Output example:

LMOutput(
    outputs=[
        LMOutputItem(type="thinking", output=Reasoning(reasoning="I'm thinking...", ...)),
        LMOutputItem(type="text", output="Golden retriever is a good dog breed."),
    ]
)

Streaming output example: python {"type": "thinking_start", "value": "", ...} {"type": "thinking", "value": "I'm ", ...} {"type": "thinking", "value": "thinking...", ...} {"type": "thinking_end", "value": "", ...} {"type": "response", "value": "Golden retriever ", ...} {"type": "response", "value": "is a good dog breed.", ...} Note: By default, the thinking token will be streamed with the legacy EventType.DATA event type. To use the new simplified streamed event format, set the simplify_events parameter to True during LM invoker initialization. The legacy event format support will be removed in v0.6.

Setting reasoning-related parameters for non-reasoning models will raise an error.

Analytics tracking

The OpenAIChatCompletionsLMInvoker can be configured to output additional information about the invocation. This feature can be enabled by setting the output_analytics parameter to True.

When enabled, the following attributes will be stored in the output: 1. token_usage: The token usage. 2. duration: The duration in seconds. 3. finish_details: The details about how the generation finished.

Output example:

LMOutput(
    outputs=[...],
    token_usage=TokenUsage(input_tokens=100, output_tokens=50),
    duration=0.729,
    finish_details={"stop_reason": "end_turn"},
)

When streaming is enabled, token usage is not supported.

Retry and timeout

The OpenAIChatCompletionsLMInvoker supports retry and timeout configuration. By default, the max retries is set to 0 and the timeout is set to 30.0 seconds. They can be customized by providing a custom RetryConfig object to the retry_config parameter.

Retry config examples:

retry_config = RetryConfig(max_retries=0, timeout=None)  # No retry, no timeout
retry_config = RetryConfig(max_retries=5, timeout=10.0)  # 5 max retries, 10.0 seconds timeout

Usage example:

lm_invoker = OpenAIChatCompletionsLMInvoker(..., retry_config=retry_config)

Initializes a new instance of the OpenAIChatCompletionsLMInvoker class.

Parameters:

Name Type Description Default
model_name str

The name of the OpenAI model.

required
api_key str | None

The API key for authenticating with OpenAI. Defaults to None, in which case the OPENAI_API_KEY environment variable will be used. If the endpoint does not require an API key, a dummy value can be passed (e.g. "").

None
base_url str

The base URL of a custom endpoint that is compatible with OpenAI's Chat Completions API schema. Defaults to OpenAI's default URL.

OPENAI_DEFAULT_URL
model_kwargs dict[str, Any] | None

Additional model parameters. Defaults to None.

None
default_hyperparameters dict[str, Any] | None

Default hyperparameters for invoking the model. Defaults to None.

None
tools list[Tool | Tool] | None

Tools provided to the model to enable tool calling. Defaults to None.

None
response_schema ResponseSchema | None

The schema of the response. If provided, the model will output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary. Defaults to None.

None
output_analytics bool

Whether to output the invocation analytics. Defaults to False.

False
retry_config RetryConfig | None

The retry configuration for the language model. Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.

None
reasoning_effort str | None

The reasoning effort for the language model. Defaults to None.

None
simplify_events bool

Temporary parameter to control the streamed events format. When True, uses the simplified events format. When False, uses the legacy events format for backward compatibility. Will be removed in v0.6. Defaults to False.

False

set_response_schema(response_schema)

Sets the response schema for the OpenAI language model.

This method sets the response schema for the OpenAI language model. Any existing response schema will be replaced.

Parameters:

Name Type Description Default
response_schema ResponseSchema | None

The response schema to be used.

required