diff --git a/strands-py/src/strands/models/openai_responses.py b/strands-py/src/strands/models/openai_responses.py index 8914fb01c0..112d952c10 100644 --- a/strands-py/src/strands/models/openai_responses.py +++ b/strands-py/src/strands/models/openai_responses.py @@ -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"}} 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": diff --git a/strands-py/tests/strands/models/test_openai_responses.py b/strands-py/tests/strands/models/test_openai_responses.py index 697508339c..66b3d22c17 100644 --- a/strands-py/tests/strands/models/test_openai_responses.py +++ b/strands-py/tests/strands/models/test_openai_responses.py @@ -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 + + @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."""