[BREAKING] Python: clean up kwargs across agents, chat clients, tools, and sessions#4581
[BREAKING] Python: clean up kwargs across agents, chat clients, tools, and sessions#4581eavanvalkenburg wants to merge 1 commit intomicrosoft:mainfrom
Conversation
cd784fb to
fbede56
Compare
There was a problem hiding this comment.
Pull request overview
Refactors Python public APIs to reduce ambiguous **kwargs usage by introducing explicit runtime buckets (function_invocation_kwargs, client_kwargs) and a first-class FunctionInvocationContext injection mechanism for tools, while updating provider/client constructors to use additional_properties and aligning docstrings/tests/samples to the new calling patterns.
Changes:
- Replaces broad
**kwargsforwarding with explicitfunction_invocation_kwargsandclient_kwargsacross agent and chat-client layers, adding compatibility deprecation warnings. - Updates
@toolruntime injection to prefer an explicit/typedFunctionInvocationContextparameter (with legacy**kwargssupport retained for now). - Refactors
Agent.as_tool()to use an explicit JSON schema, always-streaming execution, approval-mode support, and correct propagation ofuser_input_requestviaUserInputRequiredException.
Reviewed changes
Copilot reviewed 50 out of 50 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| python/samples/02-agents/tools/function_tool_with_session_injection.py | Updates sample to pass sessions via function_invocation_kwargs and consume via FunctionInvocationContext. |
| python/samples/02-agents/tools/function_tool_with_kwargs.py | Updates sample to demonstrate context-based runtime injection instead of **kwargs. |
| python/samples/02-agents/tools/agent_as_tool_with_session_propagation.py | Updates sample to explicitly pass session via function_invocation_kwargs and show propagation semantics. |
| python/packages/redis/agent_framework_redis/_history_provider.py | Adds explicit state= keyword-only parameter to history provider interface methods. |
| python/packages/ollama/agent_framework_ollama/_embedding_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/ollama/agent_framework_ollama/_chat_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/github_copilot/agent_framework_github_copilot/_agent.py | Removes unused **kwargs from agent run surface and internal helpers. |
| python/packages/foundry_local/agent_framework_foundry_local/_foundry_local_client.py | Replaces wrapper **kwargs passthrough with explicit additional_properties. |
| python/packages/durabletask/tests/test_shim.py | Removes tests that validated kwargs-forwarding behavior for session creation. |
| python/packages/durabletask/tests/test_orchestration_context.py | Removes tests relying on session creation with extra parameters. |
| python/packages/durabletask/tests/test_client.py | Removes tests relying on session creation with extra parameters. |
| python/packages/durabletask/tests/test_agent_session_id.py | Updates durable session construction/serialization tests to match new init/from_dict behavior. |
| python/packages/durabletask/agent_framework_durabletask/_shim.py | Adjusts durable shim session APIs and behavior to align with narrowed session interfaces. |
| python/packages/durabletask/agent_framework_durabletask/_models.py | Refactors DurableAgentSession initialization/serialization and durable session id handling. |
| python/packages/durabletask/agent_framework_durabletask/_executors.py | Makes durable executor session creation explicit (session_id, service_session_id). |
| python/packages/devui/agent_framework_devui/models/_discovery_models.py | Import ordering cleanup. |
| python/packages/declarative/agent_framework_declarative/_workflows/_executors_tools.py | Import ordering cleanup. |
| python/packages/core/tests/core/test_tools.py | Adds coverage for typed/untyped FunctionInvocationContext injection and validation constraints. |
| python/packages/core/tests/core/test_sessions.py | Updates test history provider to accept explicit state= kw-only arg. |
| python/packages/core/tests/core/test_kwargs_propagation_to_ai_function.py | Adds/adjusts tests to validate separation of runtime buckets and legacy kwargs compatibility. |
| python/packages/core/tests/core/test_function_invocation_logic.py | Adds test ensuring user_input_request propagates through as_tool() execution. |
| python/packages/core/tests/core/test_embedding_client.py | Adds test ensuring embedding clients reject unknown kwargs (tightening constructor surfaces). |
| python/packages/core/tests/core/test_clients.py | Adds tests for docstrings layering and explicit additional_properties/client_kwargs behaviors. |
| python/packages/core/tests/core/test_as_tool_kwargs_propagation.py | Updates tests to use FunctionInvocationContext and validate function_invocation_kwargs propagation semantics. |
| python/packages/core/tests/core/test_agents.py | Updates as_tool() expectations (schema, streaming finalization) and session/tool-kwargs semantics; adds docstring tests. |
| python/packages/core/agent_framework/openai/_chat_client.py | Adds overloads + layered docstring application for OpenAI clients while preserving runtime signature semantics. |
| python/packages/core/agent_framework/observability.py | Updates telemetry layers to flatten client_kwargs and ignore already-processed function_invocation_kwargs. |
| python/packages/core/agent_framework/exceptions.py | Introduces UserInputRequiredException for propagating user-input requests through tool/agent layers. |
| python/packages/core/agent_framework/azure/_chat_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/core/agent_framework/_types.py | Adjusts ResponseStream hook typing to allow `Awaitable[UpdateT |
| python/packages/core/agent_framework/_tools.py | Adds context injection discovery, explicit context= param for invoke, legacy kwargs deprecation warnings, and user-input propagation handling. |
| python/packages/core/agent_framework/_skills.py | Minor formatting simplification. |
| python/packages/core/agent_framework/_sessions.py | Makes state= explicit in history provider abstract methods for known data flow. |
| python/packages/core/agent_framework/_middleware.py | Splits legacy runtime kwargs from client_kwargs and function_invocation_kwargs in contexts; updates chat/agent middleware plumbing accordingly. |
| python/packages/core/agent_framework/_docstrings.py | New helper to layer/extend docstrings from raw implementations. |
| python/packages/core/agent_framework/_clients.py | Introduces docstring layering; updates BaseChatClient.get_response compatibility flattening and as_agent(additional_properties=...). |
| python/packages/core/agent_framework/_agents.py | Adds runtime bucket parameters; refactors as_tool() schema + always-streaming wrapper; adds docstring layering; deprecates direct run kwargs. |
| python/packages/core/agent_framework/init.py | Exports UserInputRequiredException. |
| python/packages/copilotstudio/agent_framework_copilotstudio/_agent.py | Removes unused **kwargs from run surface and internal helpers. |
| python/packages/claude/agent_framework_claude/_agent.py | Aligns run signature with explicit options + bucket params (ignored for this provider). |
| python/packages/bedrock/agent_framework_bedrock/_embedding_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/bedrock/agent_framework_bedrock/_chat_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/azure-cosmos/agent_framework_azure_cosmos/_history_provider.py | Adds explicit state= kw-only arg to match updated history provider interface. |
| python/packages/azure-ai/agent_framework_azure_ai/_embedding_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/azure-ai/agent_framework_azure_ai/_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/azure-ai-search/tests/test_aisearch_context_provider.py | Adds fixture to clear env vars for test isolation. |
| python/packages/anthropic/agent_framework_anthropic/_chat_client.py | Replaces constructor **kwargs passthrough with explicit additional_properties. |
| python/packages/ag-ui/tests/ag_ui/test_agent_wrapper_comprehensive.py | Updates AG-UI wrapper test to capture session at call-time rather than via client kwargs. |
| python/packages/ag-ui/tests/ag_ui/conftest.py | Updates stub client to read session from client_kwargs compatibility path. |
| python/packages/ag-ui/agent_framework_ag_ui/_client.py | Removes unused **kwargs passthrough from client constructor. |
| python/packages/a2a/agent_framework_a2a/_agent.py | Aligns run signature with explicit bucket params (ignored for this provider). |
| python/CODING_STANDARD.md | Codifies the **kwargs migration strategy and preferred explicit “buckets”. |
python/samples/02-agents/tools/agent_as_tool_with_session_propagation.py
Show resolved
Hide resolved
…ons (microsoft#3642) Audit and refactor public **kwargs usage across core agents, chat clients, tools, sessions, and provider packages per the migration strategy codified in CODING_STANDARD.md. Key changes: - Add explicit runtime buckets: function_invocation_kwargs and client_kwargs on RawAgent.run() and chat client get_response() layers. - Refactor FunctionTool to prefer explicit ctx: FunctionInvocationContext injection; legacy **kwargs tools still work via _forward_runtime_kwargs. - Refactor Agent.as_tool() to use direct JSON schema, always-streaming wrapper, approval_mode parameter, and UserInputRequiredException propagation (integrates PR microsoft#4568 behavior). - Remove implicit session bleeding into FunctionInvocationContext; tools that need a session must receive it via function_invocation_kwargs. - Lower chat-client layers after FunctionInvocationLayer accept only compatibility **kwargs (client_kwargs flattened, function_invocation_kwargs ignored). - Add layered docstring composition from Raw... implementations via _docstrings.py helper. - Clean up provider constructors to use explicit additional_properties. - Deprecation warnings on legacy direct kwargs paths. - Update samples, tests, and typing across all 23 packages. Resolves microsoft#3642 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fbede56 to
1c3ccd3
Compare
| ``session_id`` and provider-managed state, such as any stored | ||
| conversation history or metadata). Defaults to False, meaning | ||
| the sub-agent runs with a new, independent session. | ||
| propagate_session: If True, the sub-agent's ``run()`` call receives |
There was a problem hiding this comment.
Question: what does it mean here by "sub-agent"? Is it when an agent is used as a tool and this agent (the parent) is given the tool?
There was a problem hiding this comment.
Yes that is indeed meant for Agent as tool being added to another agent, not orchestrations, I can clarify
Motivation and Context
Resolves #3642 — the Python codebase relied heavily on blanket
**kwargsacross public APIs (agents, chat clients, tools, sessions), making it difficult to discover available parameters, catch mistakes at type-check time, and reason about what data flows where.Description
Audit and refactor public
**kwargsusage across core agents, chat clients, tools, sessions, and provider packages. The migration strategy is codified inCODING_STANDARD.md.Explicit runtime buckets replace blanket
**kwargsforwarding:RawAgent.run(..., function_invocation_kwargs=..., client_kwargs=...)get_response()layers use the same split; belowFunctionInvocationLayeronly compatibility**kwargsremain (client_kwargs flattened, function_invocation_kwargs ignored).FunctionTool context injection — tools now prefer an explicit
ctx: FunctionInvocationContextparameter. Legacy**kwargstools still work via_forward_runtime_kwargs.Agent.as_tool() refactor — direct JSON schema (no generated Pydantic model), always-streaming wrapper,
approval_modeparameter, andUserInputRequiredExceptionpropagation (integrates #4568 behavior).Session decoupled from function context —
FunctionInvocationContextno longer has asessionattribute. Tools that need a session must receive it explicitly viafunction_invocation_kwargs.Layered docstrings — public APIs compose docstrings from the nearest
Raw...implementation via a new_docstrings.pyhelper.Provider constructors cleaned up to use explicit
additional_properties. Deprecation warnings on all legacy direct kwargs paths.Updated samples, tests, and typing across all 23 packages.
Contribution Checklist