fix(azure_ai): keep a Foundry agent version requested at the top level when extra_body also names the agent

This commit is contained in:
mateo-berri 2026-10-28 17:14:51 -07:00
parent 0c9db4ef81
commit 092b86f2d3
2 changed files with 60 additions and 5 deletions

View file

@ -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
),
}

View file

@ -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,