openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Incomplete responses are not handled by `parse_response` when using Structured Output

Open ejm-betterup opened this issue 1 year ago • 3 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [x] This is an issue with the Python library

Describe the bug

I'm using Structured Outputs and have a max token length of 2000 for my finetuned model. When I generate a response, response.output has a length of 8 and the last output is incomplete. When response.output contains an incomplete output, parse_response doesn't handle this and instead raises a ValidationError.

It seems like there should either be a way to constrain the length of response.output or ignore incomplete outputs in parsing, for example:

if output.type == "message" and output.status != "incomplete":

instead of https://github.com/openai/openai-python/blob/main/src/openai/lib/_parsing/_responses.py#L63

To Reproduce

To repro, ensure that response.output contains an incomplete output and then call parse_response with a structured output.

raw_response.output[-1] = ResponseOutputMessage(id='xxx', content=[ResponseOutputText(annotations=[], text='{"a": {"b": 5.0, , "c": {"d": 4.0,', type='output_text', logprobs=[])], role='assistant', status='incomplete', type='message')

parse_response(
   text_format=ExpectedResponse,
   response=raw_response,
)

Code snippets

client = OpenAI() 
resp = client.responses.parse(
    model=model,
    instructions=prompt,
    input=input_text.to_list(),
    temperature=temperature,
    max_output_tokens=max_tokens
    text_format=ExpectedResponse,
)


yields


  File "/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1124, in _process_response
    return api_response.parse()
           ~~~~~~~~~~~~~~~~~~^^
  File "/venv/lib/python3.13/site-packages/openai/_response.py", line 325, in parse
    parsed = self._options.post_parser(parsed)
  File "/.venv/lib/python3.13/site-packages/openai/resources/responses/responses.py", line 936, in parser
    return parse_response(
        input_tools=tools,
        text_format=text_format,
        response=raw_response,
    )
  File "/.venv/lib/python3.13/site-packages/openai/lib/_parsing/_responses.py", line 75, in parse_response
    "parsed": parse_text(item.text, text_format=text_format),
              ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.13/site-packages/openai/lib/_parsing/_responses.py", line 137, in parse_text
    return cast(TextFormatT, model_parse_json(text_format, text))
                             ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.13/site-packages/openai/_compat.py", line 169, in model_parse_json
    return model.model_validate_json(data)
           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/.venv/lib/python3.13/site-packages/pydantic/main.py", line 746, in model_validate_json
    return cls.__pydantic_validator__.validate_json(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        json_data, strict=strict, context=context, by_alias=by_alias, by_name=by_name
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 1 validation error for ExpectedResponse
  Invalid JSON: EOF while parsing an object at line 1 column 234 [type=json_invalid, input_value='{"example_key":...n": {"clarity": 6', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/json_invalid

OS

macOS

Python version

python v3.13

Library version

openai v1.86.0

ejm-betterup avatar Jul 22 '25 04:07 ejm-betterup

Hi, I'd like to work on this. I'll submit a small PR shortly.

nightcityblade avatar May 20 '26 15:05 nightcityblade

Tracking the streaming case from #3263 here alongside the non-streaming report. The fix needs to cover:

  • responses.parse(): preserve incomplete output items and their status without trying to parse their truncated structured text, while still parsing completed siblings.
  • responses.stream(): an early response.output_text.done parsing failure must not hide the terminal response.incomplete event or its reason.
  • get_final_response(): preserve the terminal incomplete response, including its content, status, and incomplete details, for both sync and async callers.

Validation of completed structured output should remain strict. This issue stays open until both the streaming and non-streaming cases are addressed.

marcuswood-oai avatar Sep 21 '26 17:09 marcuswood-oai

This still reproduces on 3.57.0 with a recorded Responses API response (status: "incomplete", incomplete_details.reason: "max_output_tokens"): responses.parse(text_format=...) raises only a pydantic ValidationError, so the caller gets neither the status nor the usage. A self-contained repro with that recorded response, HTTP mocked, no key needed:

pip install "openai==3.57.0"
curl -sLO https://raw.githubusercontent.com/kartsan03/wiretruth/v0.4.0/suite/fixtures/openai-responses/truncation.structured-output.json
python repro.py
import json
from pathlib import Path

import httpx2
from openai import OpenAI
from pydantic import BaseModel


class Island(BaseModel):
    name: str
    sea: str


class Output(BaseModel):
    islands: list[Island]


fixture = json.loads(Path("truncation.structured-output.json").read_text())
response = fixture["exchanges"][0]["response"]
request = fixture["provenance"]["request"]
transport = httpx2.MockTransport(
    lambda _: httpx2.Response(response["status"], headers=response["headers"], json=response["body"]["json"])
)
client = OpenAI(api_key="test", http_client=httpx2.Client(transport=transport))
try:
    client.responses.parse(
        model=request["model"],
        input=request["input"],
        max_output_tokens=request["max_output_tokens"],
        reasoning=request["reasoning"],
        text_format=Output,
    )
except Exception as error:
    print(f"{type(error).__name__}: {error}")
ValidationError: 1 validation error for Output
  Invalid JSON: EOF while parsing a string at line 1 column 59 [type=json_invalid, input_value='{"islands":[{"name":"Κ...a":"Κρητικό Πέ', input_type=str]
    For further information visit https://errors.pydantic.dev/2.14/v/json_invalid

kartsan03 avatar Nov 21 '26 07:11 kartsan03