Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion strands-py/src/strands/models/openai_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,13 @@ async def structured_output(
"""
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.

response = await client.responses.parse(
model=self.get_config()["model_id"],
input=self._format_request(prompt, system_prompt=system_prompt)["input"],
input=request["input"],
text_format=output_model,
**parse_kwargs,
)
except openai.BadRequestError as e:
if hasattr(e, "code") and e.code == "context_length_exceeded":
Expand Down
23 changes: 23 additions & 0 deletions strands-py/tests/strands/models/test_openai_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,29 @@ async def test_structured_output(openai_client, model, test_output_model_cls, al
assert tru_result == exp_result


@pytest.mark.asyncio
async def test_structured_output_forwards_formatted_request_params(
openai_client, model_id, test_output_model_cls, alist
):
model = OpenAIResponsesModel(
model_id=model_id,
params={"max_output_tokens": 1000, "reasoning": {"effort": "high"}},
)
messages = [{"role": "user", "content": [{"text": "Generate a person"}]}]
mock_response = unittest.mock.Mock(output_parsed=test_output_model_cls(name="John", age=30))

openai_client.responses.parse = unittest.mock.AsyncMock(return_value=mock_response)

await alist(model.structured_output(test_output_model_cls, messages, system_prompt="Follow the schema"))

_, kwargs = openai_client.responses.parse.call_args
assert kwargs["max_output_tokens"] == 1000
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



@pytest.mark.asyncio
async def test_stream_context_overflow_exception(openai_client, model, messages):
"""Test that OpenAI context overflow errors are properly converted to ContextWindowOverflowException."""
Expand Down
Loading