From a22b91fb5907b2fad45fba4674c6337e35b3b370 Mon Sep 17 00:00:00 2001 From: nickhac Date: Mon, 17 Aug 2026 09:11:37 +0000 Subject: [PATCH] fix(missing_documentation): Def `parser` in src/openai/resources/chat/completions/comple Gap ID: #332 Co-authored-by: Hermes Agent --- .../resources/chat/completions/completions.py | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/openai/resources/chat/completions/completions.py b/src/openai/resources/chat/completions/completions.py index a2e36b6ad2..de5a1e0fe2 100644 --- a/src/openai/resources/chat/completions/completions.py +++ b/src/openai/resources/chat/completions/completions.py @@ -184,6 +184,43 @@ class MathResponse(BaseModel): } def parser(raw_completion: ChatCompletion) -> ParsedChatCompletion[ResponseFormatT]: + """Post-process a raw ``ChatCompletion`` into a ``ParsedChatCompletion``. + + This callback is passed as ``post_parser`` to the underlying HTTP request + helper. It is called once the API response has been deserialised into a + ``ChatCompletion`` object and converts it into the richer + ``ParsedChatCompletion`` type, including structured-output parsing of + ``response_format`` and automatic argument parsing for any tool calls that + were constructed with :func:`openai.pydantic_function_tool` or marked with + ``"strict": True``. + + Args: + raw_completion: The deserialised ``ChatCompletion`` returned by the + Chat Completions API before structured-output post-processing. + + Returns: + A ``ParsedChatCompletion[ResponseFormatT]`` whose + ``choices[*].message.parsed`` field holds the decoded + ``ResponseFormatT`` instance (when a ``response_format`` was + supplied), and whose tool-call ``function.parsed_arguments`` + fields hold decoded pydantic models where applicable. + + Example:: + + from pydantic import BaseModel + from openai import OpenAI + + class Answer(BaseModel): + value: str + + client = OpenAI() + completion = client.chat.completions.parse( + model="gpt-4o-2024-08-06", + messages=[{"role": "user", "content": "What is 1 + 1?"}], + response_format=Answer, + ) + print(completion.choices[0].message.parsed) + """ return _parse_chat_completion( response_format=response_format, chat_completion=raw_completion, @@ -1795,6 +1832,43 @@ class MathResponse(BaseModel): } def parser(raw_completion: ChatCompletion) -> ParsedChatCompletion[ResponseFormatT]: + """Post-process a raw ``ChatCompletion`` into a ``ParsedChatCompletion``. + + This callback is passed as ``post_parser`` to the underlying HTTP request + helper. It is called once the API response has been deserialised into a + ``ChatCompletion`` object and converts it into the richer + ``ParsedChatCompletion`` type, including structured-output parsing of + ``response_format`` and automatic argument parsing for any tool calls that + were constructed with :func:`openai.pydantic_function_tool` or marked with + ``"strict": True``. + + Args: + raw_completion: The deserialised ``ChatCompletion`` returned by the + Chat Completions API before structured-output post-processing. + + Returns: + A ``ParsedChatCompletion[ResponseFormatT]`` whose + ``choices[*].message.parsed`` field holds the decoded + ``ResponseFormatT`` instance (when a ``response_format`` was + supplied), and whose tool-call ``function.parsed_arguments`` + fields hold decoded pydantic models where applicable. + + Example:: + + from pydantic import BaseModel + from openai import AsyncOpenAI + + class Answer(BaseModel): + value: str + + client = AsyncOpenAI() + completion = await client.chat.completions.parse( + model="gpt-4o-2024-08-06", + messages=[{"role": "user", "content": "What is 1 + 1?"}], + response_format=Answer, + ) + print(completion.choices[0].message.parsed) + """ return _parse_chat_completion( response_format=response_format, chat_completion=raw_completion,