Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions src/openai/resources/chat/completions/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,91 @@ class MathResponse(BaseModel):
print(message.parsed.steps)
print("answer: ", message.parsed.final_answer)
```

Args:
messages: A list of messages comprising the conversation so far. Depending on the
[model](https://platform.openai.com/docs/models) you use, different message
types (modalities) are supported, like text, images, and audio.

model: Model ID used to generate the response, like `gpt-4o` or `o3`. Refer to the
[model guide](https://platform.openai.com/docs/models) to browse available
models.

response_format: A Pydantic model class or other type that the response content should be

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict response-format guidance to supported types

When a caller interprets “other type” as an ordinary class or TypedDict, _type_to_response_format() raises TypeError before sending the request; it accepts only pydantic.BaseModel subclasses and Pydantic dataclass-like types, with the latter requiring Pydantic v2. Name those supported alternatives instead of implying that an arbitrary type can be parsed.

Useful? React with 👍 / 👎.

parsed into. When provided, the method automatically constructs the JSON schema
from the type and sends it to the API as a `json_schema` response format, then
deserialises the returned JSON into an instance of that type. Omit this
argument (or pass `openai.NOT_GIVEN`) to receive a plain
`ParsedChatCompletion` without automatic deserialisation.
Comment on lines +191 to +193

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recommend a sentinel accepted by the public signature

When a typed caller follows this advice, response_format=openai.NOT_GIVEN is rejected because NOT_GIVEN is a NotGiven, while the public parameter accepts only type[ResponseFormatT] | Omit. Recommend omitting the argument or passing openai.omit, unless NotGiven is deliberately added to the public signature.

Useful? React with 👍 / 👎.


tools: A list of tools the model may call. Pass tool schemas created with
`openai.pydantic_function_tool()` to enable automatic parsing of tool-call
arguments into the corresponding Pydantic model instances.
Comment on lines +195 to +197

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge State that parse rejects unsupported tool schemas

When a caller supplies a custom tool or an ordinary non-strict function tool, _validate_input_tools() raises ValueError before the request; these are not merely left unparsed. This generic description should state that parse() accepts only strict function tools, including those produced by pydantic_function_tool().

Useful? React with 👍 / 👎.


audio: Parameters for audio output. Required when audio output is requested with
`modalities: ["audio"]`.

frequency_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
their existing frequency in the text so far, decreasing the model's likelihood
to repeat the same line verbatim.

logit_bias: Modify the likelihood of specified tokens appearing in the completion.

logprobs: Whether to return log probabilities of the output tokens or not.

max_completion_tokens: An upper bound for the number of tokens that can be generated for a
completion, including visible output tokens and reasoning tokens.

max_tokens: The maximum number of tokens that can be generated in the chat completion.
Deprecated in favour of `max_completion_tokens`.
Comment on lines +213 to +214

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Warn that max_tokens is incompatible with o-series models

When a caller uses an o-series model such as the documented o3 example, this description does not disclose that max_tokens is incompatible with those models. The canonical create() documentation states that restriction explicitly, so direct these callers to max_completion_tokens rather than merely labeling the old parameter deprecated.

Useful? React with 👍 / 👎.


n: How many chat completion choices to generate for each input message. Note that
you will be charged based on the number of generated tokens across all of the
choices. Keep `n` as `1` to minimise costs.

parallel_tool_calls: Whether to enable parallel function calling during tool use.

presence_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
whether they appear in the text so far, increasing the model's likelihood to
talk about new topics.

reasoning_effort: Constrains effort on reasoning for reasoning models. Supported values are
`low`, `medium`, and `high`.
Comment on lines +226 to +227

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Document all supported reasoning-effort values

When users need the newer reasoning modes, this incorrectly limits the supported values to low, medium, and high. The ReasoningEffort type used by this method and the canonical create() documentation also support none, minimal, xhigh, and max subject to model-specific availability, so this documentation hides valid options.

Useful? React with 👍 / 👎.


seed: If specified, the system will make a best effort to sample deterministically.

service_tier: Specifies the processing type used for serving the request.

stop: Up to 4 sequences where the API will stop generating further tokens.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Qualify stop-sequence support for reasoning models

When parse() is used with o3 or o4-mini, this unqualified statement suggests that stop is supported, while the same endpoint's generated create() documentation explicitly says it is unsupported for those models. Carry that qualification into the helper documentation so callers do not construct unsupported requests.

Useful? React with 👍 / 👎.


store: Whether to store the output of this chat completion request for use in model
distillation or evals products.

temperature: Sampling temperature between 0 and 2.

tool_choice: Controls which (if any) tool is called by the model.

top_logprobs: An integer between 0 and 20 specifying the number of most likely tokens to
return at each token position. `logprobs` must be `true` when used.
Comment on lines +242 to +243

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Describe top_logprobs as an upper bound

When consumers use this value to size or index the returned alternatives, the API may return fewer entries than requested. The canonical endpoint documentation defines top_logprobs as the maximum number of likely tokens and explicitly allows fewer results, so saying it specifies “the number” incorrectly implies an exact count.

Useful? React with 👍 / 👎.


top_p: Nucleus sampling probability mass.

user: A stable identifier for your end-users.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Direct user callers to its replacement fields

When a new caller uses user for cache bucketing or abuse detection based on this description, they miss that the field is being replaced. The canonical endpoint documentation directs cache use to prompt_cache_key and abuse-detection identity to safety_identifier; include that transition here so the new helper documentation does not steer integrations toward the legacy field.

Useful? React with 👍 / 👎.


extra_headers: Send extra headers

extra_query: Add additional query parameters to the request

extra_body: Add additional JSON properties to the request

timeout: Override the client-level default timeout for this request, in seconds

Returns:
A `ParsedChatCompletion[ResponseFormatT]` object. When `response_format` is
provided the `.choices[i].message.parsed` attribute contains the deserialised
response as an instance of `ResponseFormatT`. Tool calls made with
Comment on lines +258 to +260

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the optionality of message.parsed

When the model refuses the request or returns no content, maybe_parse_content() leaves message.parsed as None even though response_format was provided. This return documentation promises an instance unconditionally, which can lead callers to dereference parsed without the guard already shown in the example; qualify the statement for non-refusal responses with parseable content.

Useful? React with 👍 / 👎.

`pydantic_function_tool()` will have their `.function.parsed_arguments`
attribute populated with the corresponding Pydantic model instance.
"""
chat_completion_tools = _validate_input_tools(tools)

Expand Down Expand Up @@ -1786,6 +1871,91 @@ class MathResponse(BaseModel):
print(message.parsed.steps)
print("answer: ", message.parsed.final_answer)
```

Args:
messages: A list of messages comprising the conversation so far. Depending on the
[model](https://platform.openai.com/docs/models) you use, different message
types (modalities) are supported, like text, images, and audio.

model: Model ID used to generate the response, like `gpt-4o` or `o3`. Refer to the
[model guide](https://platform.openai.com/docs/models) to browse available
models.

response_format: A Pydantic model class or other type that the response content should be
parsed into. When provided, the method automatically constructs the JSON schema
from the type and sends it to the API as a `json_schema` response format, then
deserialises the returned JSON into an instance of that type. Omit this
argument (or pass `openai.NOT_GIVEN`) to receive a plain
`ParsedChatCompletion` without automatic deserialisation.

tools: A list of tools the model may call. Pass tool schemas created with
`openai.pydantic_function_tool()` to enable automatic parsing of tool-call
arguments into the corresponding Pydantic model instances.

audio: Parameters for audio output. Required when audio output is requested with
`modalities: ["audio"]`.

frequency_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
their existing frequency in the text so far, decreasing the model's likelihood
to repeat the same line verbatim.

logit_bias: Modify the likelihood of specified tokens appearing in the completion.

logprobs: Whether to return log probabilities of the output tokens or not.

max_completion_tokens: An upper bound for the number of tokens that can be generated for a
completion, including visible output tokens and reasoning tokens.

max_tokens: The maximum number of tokens that can be generated in the chat completion.
Deprecated in favour of `max_completion_tokens`.

n: How many chat completion choices to generate for each input message. Note that
you will be charged based on the number of generated tokens across all of the
choices. Keep `n` as `1` to minimise costs.

parallel_tool_calls: Whether to enable parallel function calling during tool use.

presence_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
whether they appear in the text so far, increasing the model's likelihood to
talk about new topics.

reasoning_effort: Constrains effort on reasoning for reasoning models. Supported values are
`low`, `medium`, and `high`.

seed: If specified, the system will make a best effort to sample deterministically.

service_tier: Specifies the processing type used for serving the request.

stop: Up to 4 sequences where the API will stop generating further tokens.

store: Whether to store the output of this chat completion request for use in model
distillation or evals products.

temperature: Sampling temperature between 0 and 2.

tool_choice: Controls which (if any) tool is called by the model.

top_logprobs: An integer between 0 and 20 specifying the number of most likely tokens to
return at each token position. `logprobs` must be `true` when used.

top_p: Nucleus sampling probability mass.

user: A stable identifier for your end-users.

extra_headers: Send extra headers

extra_query: Add additional query parameters to the request

extra_body: Add additional JSON properties to the request

timeout: Override the client-level default timeout for this request, in seconds

Returns:
A `ParsedChatCompletion[ResponseFormatT]` object. When `response_format` is
provided the `.choices[i].message.parsed` attribute contains the deserialised
response as an instance of `ResponseFormatT`. Tool calls made with
`pydantic_function_tool()` will have their `.function.parsed_arguments`
attribute populated with the corresponding Pydantic model instance.
"""
_validate_input_tools(tools)

Expand Down