OpenAIChatCompletionsModel wraps OpenAI's AsyncOpenAI client to implement the Model interface, supporting chat completions with configurable feature validation that either rejects or silently ignores unsupported fields like reusable prompts and certain reasoning modes. The model logs responses at DEBUG level (when enabled), wraps calls in tracing spans with error capture, and uses shielded background cleanup to safely close streams without abandoning in-progress close operations.
OpenAIChatCompletionsModel in src/agents/models/openai_chatcompletions.py implements the Model interface and wraps an AsyncOpenAI client to provide chat-completions-based model responses.[1]
OpenAIChatCompletionsModel accepts a strict_feature_validation flag at construction time: when True, unsupported features raise UserError; when False (the default), they are warned once and silently ignored.[1] The prompt (reusable prompt) parameter is not supported by OpenAIChatCompletionsModel; reusable prompts require the Responses API — see OpenAI Responses model.[1] Of the ModelSettings.reasoning fields, OpenAIChatCompletionsModel supports only reasoning.effort; the reasoning.mode and reasoning.context fields are unsupported and are either rejected with UserError (strict mode) or silently dropped.[1] For official OpenAI clients, OpenAIChatCompletionsModel validates that user-message content parts use only the supported types (input_text, input_image, input_audio, input_file), raising UserError on any other type.[1]
OpenAIChatCompletionsModel.get_response raises ModelBehaviorError when the provider returns a ChatCompletion with no choices, and includes the provider error payload in the error message if one is present.[1] When _debug.DONT_LOG_MODEL_DATA is falsy, OpenAIChatCompletionsModel.get_response logs the full model response message as pretty-printed JSON at DEBUG level; when truthy, it logs only a redacted "Received model response" string.[1] OpenAIChatCompletionsModel.get_response wraps every call in a generation_span tracing context and a model_span_errors error-capture context from src/agents/tracing/.[1]
OpenAIChatCompletionsModel._close_stream_allowing_background_completion shields the provider stream's aclose() call so that cancellation during close does not abandon a half-finished close operation; the task is detached to finish in the background, avoiding a second close (which is not guaranteed to be safe or idempotent).[1] OpenAIChatCompletionsModel.get_retry_advice delegates to get_openai_retry_advice in src/agents/models/_openai_retry.py to determine whether and how to retry a failed request.[1]
Sources