openai / openai/openai-python

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

Open Beginner friendly
#2,486 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
31.6k
Forks
5.7k
Avg merge
1d 6h
Merged PRs (30d)
96

Description

Confirm this is an issue with the Python library and not an underlying OpenAI API
  • 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/openai/lib/_parsing/_responses.py at parse_response and reproduce the incomplete ResponseOutputMessage shown in the issue. Check how existing response parsing handles message status and identify the expected behavior for incomplete structured-output items. Done means the reproduction no longer raises a validation error for an incomplete output, with coverage for that case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.