From 092b86f2d3c7155df097cf8c7493981a18f7bbcb Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 17:14:51 -0700 Subject: [PATCH] fix(azure_ai): keep a Foundry agent version requested at the top level when extra_body also names the agent --- .../llms/azure_ai/responses/transformation.py | 21 ++++++--- .../test_azure_ai_responses_transformation.py | 44 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/litellm/llms/azure_ai/responses/transformation.py b/litellm/llms/azure_ai/responses/transformation.py index eafe95153a6..de3bf16de91 100644 --- a/litellm/llms/azure_ai/responses/transformation.py +++ b/litellm/llms/azure_ai/responses/transformation.py @@ -174,11 +174,15 @@ def _requested_agent( ) +def _caller_agent_fields(fields: Mapping[str, object]) -> tuple[tuple[str, object], ...]: + return tuple((key, fields[key]) for key in _CALLER_AGENT_KEYS if key in fields) + + def _deployment_agent( - model: str, caller_fields: Mapping[str, object], litellm_params: GenericLiteLLMParams + model: str, caller_fields: Sequence[tuple[str, object]], litellm_params: GenericLiteLLMParams ) -> FoundryAgentReference: - caller_keys: Final = tuple(key for key in _CALLER_AGENT_KEYS if key in caller_fields) - caller_agents: Final = tuple(_caller_agent_reference(caller_fields[key]) for key in caller_keys) + caller_keys: Final = tuple(key for key, _ in caller_fields) + caller_agents: Final = tuple(_caller_agent_reference(value) for _, value in caller_fields) agent: Final = _requested_agent(_agent_reference(model), caller_agents) other_agents: Final = tuple( key @@ -272,7 +276,9 @@ class AzureAIAgentResponsesAPIConfig(AzureAIResponsesAPIConfig): if isinstance(instructions, str) and instructions else {} ), - "agent_reference": _deployment_agent(model, litellm_params.model_extra or {}, litellm_params), + "agent_reference": _deployment_agent( + model, _caller_agent_fields(litellm_params.model_extra or {}), litellm_params + ), } def transform_extra_body( @@ -285,4 +291,9 @@ class AzureAIAgentResponsesAPIConfig(AzureAIResponsesAPIConfig): passthrough: Final = {key: value for key, value in extra_body.items() if key not in _CALLER_AGENT_KEYS} if len(passthrough) == len(extra_body): return passthrough - return {**passthrough, "agent_reference": _deployment_agent(model, extra_body, litellm_params)} + return { + **passthrough, + "agent_reference": _deployment_agent( + model, (*_caller_agent_fields(request), *_caller_agent_fields(extra_body)), litellm_params + ), + } diff --git a/tests/unit/llms/azure_ai/responses/test_azure_ai_responses_transformation.py b/tests/unit/llms/azure_ai/responses/test_azure_ai_responses_transformation.py index bbe5ea2066e..efc5d4c70d2 100644 --- a/tests/unit/llms/azure_ai/responses/test_azure_ai_responses_transformation.py +++ b/tests/unit/llms/azure_ai/responses/test_azure_ai_responses_transformation.py @@ -539,6 +539,50 @@ async def test_router_aresponses_rejects_another_version_of_a_pinned_agent_witho assert not route.called +VERSION_THREE: Final = {"name": "support-agent", "version": "3"} +NAME_ONLY: Final = {"name": "support-agent"} + + +@pytest.mark.asyncio +@respx.mock +@pytest.mark.parametrize( + "top_level,in_extra_body", + [ + ({"agent_reference": VERSION_THREE}, {"agent_reference": NAME_ONLY}), + ({"agent": VERSION_THREE}, {"agent_reference": NAME_ONLY}), + ({"agent_reference": NAME_ONLY}, {"agent_reference": VERSION_THREE}), + ], +) +async def test_router_aresponses_keeps_a_version_requested_at_the_top_level_or_in_extra_body(top_level, in_extra_body): + route = respx.post(url__regex=r".*/openai/v1/responses(\?.*)?$").mock( + return_value=httpx.Response(200, json=_responses_payload("gpt-4.1")) + ) + + await _support_agent_router().aresponses( + model="support-agent", input="What is 2+2?", **top_level, extra_body={**in_extra_body, "store": False} + ) + + body = json.loads(route.calls.last.request.content) + assert body["agent_reference"] == {"type": "agent_reference", **VERSION_THREE} + assert body["store"] is False + + +@pytest.mark.asyncio +@respx.mock +async def test_router_aresponses_rejects_different_versions_at_the_top_level_and_in_extra_body(): + route = respx.post(url__regex=r".*/openai/v1/responses(\?.*)?$") + + with pytest.raises(litellm.UnsupportedParamsError, match="drop_params"): + await _support_agent_router().aresponses( + model="support-agent", + input="What is 2+2?", + agent_reference=VERSION_THREE, + extra_body={"agent_reference": {"name": "support-agent", "version": "4"}}, + ) + + assert not route.called + + AGENT_OWNED_PARAMS = { "temperature": 0.2, "top_p": 0.5,