Skip to content

Commit 9819191

Browse files
committed
Preserve automatic tools for Foundry Local text responses
Omit explicit text response formats from HTTP requests so SDK 2.2.1 does not disable tool selection. Cover omitted, string and dictionary automatic choices in streaming and non-streaming tool loops without mutating caller options. Co-authored-by: Copilot <[email protected]> Copilot-Session: 02674608-bd9c-4c05-a2be-223196e5bd45
1 parent 3a51775 commit 9819191

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎python/packages/foundry_local/agent_framework_foundry_local/_foundry_local_client.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,17 @@ def _prepare_options(self, messages: Sequence[Message], options: Mapping[str, An
318318
prepared["max_tokens"] = prepared.pop("max_completion_tokens")
319319
response_format: Mapping[str, Any] | None = prepared.get("response_format")
320320
if isinstance(response_format, Mapping):
321-
if response_format.get("type") == "json_schema":
321+
if response_format.get("type") == "text":
322+
# Explicit text format disables automatic tool selection in the native service.
323+
prepared.pop("response_format")
324+
elif response_format.get("type") == "json_schema":
322325
schema: Mapping[str, Any] | None = response_format.get("json_schema")
323326
if isinstance(schema, Mapping):
324327
prepared["response_format"] = {
325328
**response_format,
326329
"json_schema": schema.get("schema", schema),
327330
}
328-
elif response_format.get("type") not in ("json_object", "text"):
331+
elif response_format.get("type") != "json_object":
329332
# Older OpenAI adapters pass raw JSON schemas through unchanged.
330333
prepared["response_format"] = {"type": "json_schema", "json_schema": response_format}
331334
tool_choice: str | Mapping[str, Any] | None = prepared.get("tool_choice")

‎python/packages/foundry_local/tests/test_foundry_local_client.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,9 +1216,10 @@ def respond(request: Any) -> Any:
12161216

12171217
@pytest.mark.parametrize("stream", [False, True])
12181218
@pytest.mark.parametrize("enabled", [False, True])
1219-
@pytest.mark.parametrize("tool_choice", [None, "auto", "required"])
1219+
@pytest.mark.parametrize("tool_choice", [None, "auto", {"mode": "auto", "allowed_tools": ["add"]}, "required"])
1220+
@pytest.mark.parametrize("explicit_text_format", [False, True])
12201221
async def test_foundry_local_agent_tool_loop(
1221-
mock_foundry_local_manager: MagicMock, stream: bool, enabled: bool, tool_choice: Literal["auto", "required"] | None
1222+
mock_foundry_local_manager: MagicMock, stream: bool, enabled: bool, tool_choice: Any, explicit_text_format: bool
12221223
) -> None:
12231224
payloads: list[dict[str, Any]] = []
12241225
invocations: list[tuple[int, int]] = []
@@ -1251,6 +1252,9 @@ def respond(request: Any) -> Any:
12511252
options: ChatOptions[None] = {"allow_multiple_tool_calls": False}
12521253
if tool_choice is not None:
12531254
options["tool_choice"] = tool_choice
1255+
if explicit_text_format:
1256+
options["response_format"] = {"type": "text"}
1257+
original_options = deepcopy(options)
12541258
async with _client_with_transport(respond, function_invocation_configuration={"enabled": enabled}) as client:
12551259
agent = Agent(client=client, tools=add, instructions="Use the calculator.")
12561260
if stream:
@@ -1259,8 +1263,10 @@ def respond(request: Any) -> Any:
12591263
response = await agent.run("What is 2+3?", options=options)
12601264

12611265
assert payloads[0]["tools"][0]["function"]["name"] == "add"
1262-
assert payloads[0]["tool_choice"] == (tool_choice or "auto")
1266+
assert payloads[0]["tool_choice"] == ("required" if tool_choice == "required" else "auto")
12631267
assert payloads[0]["parallel_tool_calls"] is False
1268+
assert all("response_format" not in payload for payload in payloads)
1269+
assert options == original_options
12641270
if enabled:
12651271
assert invocations == [(2, 3)]
12661272
assert len(payloads) == 2

0 commit comments

Comments
 (0)