Skip to content

fix: forward OpenAI Responses parse params#2341

Open
he-yufeng wants to merge 1 commit into
strands-agents:mainfrom
he-yufeng:fix/openai-responses-parse-params
Open

fix: forward OpenAI Responses parse params#2341
he-yufeng wants to merge 1 commit into
strands-agents:mainfrom
he-yufeng:fix/openai-responses-parse-params

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Motivation

OpenAIResponsesModel.structured_output() builds the request through _format_request(), but then only forwards input to client.responses.parse(). That drops regular Responses API parameters from model config, including max_output_tokens, reasoning, and system instructions.

Closes #1908.

Changes

  • Build the structured-output parse request from the full _format_request() result.
  • Continue passing model, input, and text_format explicitly.
  • Exclude stream, since _format_request() sets it for streaming calls and responses.parse() should not receive it.
  • Add regression coverage for max_output_tokens, reasoning, instructions, and store.

Testing

  • .\.venv\Scripts\python.exe -m pytest tests\strands\models\test_openai_responses.py::test_structured_output tests\strands\models\test_openai_responses.py::test_structured_output_forwards_formatted_request_params -q
  • .\.venv\Scripts\python.exe -m ruff check src\strands\models\openai_responses.py tests\strands\models\test_openai_responses.py
  • .\.venv\Scripts\python.exe -m ruff format --check src\strands\models\openai_responses.py tests\strands\models\test_openai_responses.py
  • .\.venv\Scripts\python.exe -m mypy src\strands\models\openai_responses.py
  • .\strands-py\.venv\Scripts\python.exe -m py_compile strands-py\src\strands\models\openai_responses.py strands-py\tests\strands\models\test_openai_responses.py
  • git diff --check

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

async with openai.AsyncOpenAI(**self._resolve_client_args()) as client:
try:
request = self._format_request(prompt, system_prompt=system_prompt)
parse_kwargs = {key: value for key, value in request.items() if key not in {"model", "input", "stream"}}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Issue: The exclusion-list approach (key not in {"model", "input", "stream"}) is fragile. If _format_request is later extended to add more keys that shouldn't reach responses.parse() (or if users put keys like tools in params for built-in tool use), those will be forwarded silently.

Suggestion: Consider using an allowlist of known safe parameters to forward, or additionally exclude keys that are only relevant to streaming/stateful flows (e.g., previous_response_id, tools, tool_choice):

_PARSE_EXCLUDE_KEYS = {"model", "input", "stream", "tools", "tool_choice", "previous_response_id"}
parse_kwargs = {k: v for k, v in request.items() if k not in _PARSE_EXCLUDE_KEYS}

This would prevent built-in tools configured via params (e.g., web_search) from leaking into structured output calls where they're unlikely to be intended.

assert kwargs["reasoning"] == {"effort": "high"}
assert kwargs["instructions"] == "Follow the schema"
assert kwargs["store"] is False
assert "stream" not in kwargs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Issue: The test asserts individual fields but doesn't verify there are no unexpected extra keys being forwarded. If a future change to _format_request adds a new key, this test wouldn't catch it being unintentionally passed through.

Suggestion: Add an assertion on the complete set of forwarded keys to catch unintended leakage:

expected_keys = {"model", "input", "text_format", "max_output_tokens", "reasoning", "instructions", "store"}
assert set(kwargs.keys()) == expected_keys

@github-actions
Copy link
Copy Markdown
Contributor

Assessment: Comment

Good targeted fix that correctly addresses the issue of dropped parameters in structured_output(). The approach of building from _format_request() and excluding inapplicable keys is sound.

Review Themes
  • Robustness: The exclusion-list approach could benefit from being more defensive against keys that shouldn't reach responses.parse() (e.g., tools, tool_choice from built-in tool configs in params).
  • Test coverage: Assertions could be tightened to verify the full set of forwarded keys rather than only checking expected ones are present, catching unintended parameter leakage.

Clean, minimal fix with good regression coverage.

@yonib05 yonib05 added area-provider Related to model providers area-structured-output Related to the structured output api bug Something isn't working python Pull requests that update python code labels May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-provider Related to model providers area-structured-output Related to the structured output api bug Something isn't working python Pull requests that update python code size/s

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] OpenAIResponsesModel.structured_output() drops config params (temperature, reasoning, etc.)

2 participants