Python: Strip tools from Foundry agent request on the preview path (allow_preview=True)#6644
Conversation
The Foundry service rejects requests that include tool declarations when an agent is specified (HTTP 400 invalid_payload, "Not allowed when agent is specified."). RawFoundryAgentChatClient._prepare_options stripped tools, tool_choice, and parallel_tool_calls only on the non-preview path, so when allow_preview=True (where the agent identity is bound on the OpenAI client via get_openai_client(agent_name=...)) the tool fields were still sent and the call failed. This client always targets a pre-provisioned agent, so it must never send tool declarations. Drop the tool fields unconditionally and log a single warning when the caller supplied tools, noting they are used only for client-side function dispatch. The non-agent FoundryChatClient (model-based) is unaffected. Fixes microsoft#5130.
|
@vaibhav-patel please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
This PR fixes Foundry agent requests failing with HTTP 400 invalid_payload when callers provide tools while using the hosted-agent “preview” path (allow_preview=True). Since RawFoundryAgentChatClient always targets a pre-provisioned Foundry agent (agent identity is specified either via extra_body.agent_reference or via the OpenAI client’s agent_name binding), the client must never send tool declarations in the request body.
Changes:
- Always strip
tools,tool_choice, andparallel_tool_callsfrom the outgoing request options (preview and non-preview paths). - Emit a single warning when tool declarations are provided, clarifying they’re only used for client-side function dispatch.
- Extend/add unit tests to cover the warning behavior and the
allow_preview=Trueregression.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/packages/foundry/agent_framework_foundry/_agent.py |
Removes the preview-only gate so tool-related fields are always stripped; adds a warning when tools are dropped. |
python/packages/foundry/tests/foundry/test_foundry_agent.py |
Updates/extends tests to assert warning emission, adds regression coverage for allow_preview=True, and validates no warning when no tools are provided. |
Summary
FoundryAgent/RawFoundryAgentChatClientstill returns HTTP 400invalid_payload("Not allowed when agent is specified.", param
tools) when tools are passed andallow_preview=True.PR #5101 added stripping of
tools/tool_choice/parallel_tool_callsinRawFoundryAgentChatClient._prepare_options(), but gated it behindif not self.allow_preview:. The preview path also specifies an agent — the identityis bound on the OpenAI client via
project_client.get_openai_client(agent_name=...)—so tool declarations are still rejected there. Since
agent_nameis required for thisclient, it always targets a pre-provisioned agent and must never send tools.
Changes
allow_previewgate so tool fields are stripped from the request body onboth the preview and non-preview paths.
logger.warningwhen the caller supplied tools, clarifying they areused only for client-side function dispatch and that tool schemas must live on the
agent definition in the service.
the
allow_preview=Truepath and a test that no warning is emitted when no tools arepassed.
The model-based
FoundryChatClient(noagent_reference) is unaffected — it stillsends tools.
Testing
uv run pytest packages/foundry/tests/foundry/test_foundry_agent.py→ 51 passed,3 skipped (live-Azure integration tests).
Fixes #5130.