Skip to content

Commit b481ac4

Browse files
Python typeddict based options (#818)
* updated for the new options setup * updated some guidance and changed AgentRunResponse to AgentResponse * added toc * fixes * added version * linting * fixed openai
1 parent 594ea56 commit b481ac4

12 files changed

Lines changed: 803 additions & 69 deletions

File tree

agent-framework/migration-guide/from-autogen/index.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,33 +221,39 @@ def get_time() -> str:
221221
client = OpenAIChatClient(model_id="gpt-5")
222222

223223
async def example():
224-
# Direct creation
224+
# Direct creation with default options
225225
agent = ChatAgent(
226226
name="assistant",
227227
chat_client=client,
228228
instructions="You are a helpful assistant.",
229-
tools=[get_weather] # Multi-turn by default
229+
tools=[get_weather], # Multi-turn by default
230+
default_options={
231+
"temperature": 0.7,
232+
"max_tokens": 1000,
233+
}
230234
)
231235

232236
# Factory method (more convenient)
233237
agent = client.create_agent(
234238
name="assistant",
235239
instructions="You are a helpful assistant.",
236-
tools=[get_weather]
240+
tools=[get_weather],
241+
default_options={"temperature": 0.7}
237242
)
238243

239-
# Execution with runtime tool configuration
244+
# Execution with runtime tool and options configuration
240245
result = await agent.run(
241246
"What's the weather?",
242-
tools=[get_time], # Can add tools at runtime
243-
tool_choice="auto"
247+
tools=[get_time], # Can add tools at runtime (keyword arg)
248+
options={"tool_choice": "auto"} # Other options go in options dict
244249
)
245250
```
246251

247252
**Key Differences:**
248253

249254
- **Default behavior**: `ChatAgent` automatically iterates through tool calls, while `AssistantAgent` requires explicit `max_tool_iterations` setting
250-
- **Runtime configuration**: `ChatAgent.run()` accepts `tools` and `tool_choice` parameters for per-invocation customization
255+
- **Runtime configuration**: `ChatAgent.run()` accepts `tools` as a keyword argument and other options via the `options` dict parameter for per-invocation customization
256+
- **Options system**: Agent Framework uses TypedDict-based options (e.g., `OpenAIChatOptions`) for type safety and IDE autocomplete. Options are passed via `default_options` at construction and `options` at runtime
251257
- **Factory methods**: Agent Framework provides convenient factory methods directly from chat clients
252258
- **State management**: `ChatAgent` is stateless and doesn't maintain conversation history between invocations, unlike `AssistantAgent` which maintains conversation history as part of its state
253259

@@ -340,18 +346,21 @@ async for event in agent.run_stream(task="Hello"):
340346
```python
341347
# Assume we have client, agent, and tools from previous examples
342348
async def streaming_example():
343-
# Chat client streaming
344-
async for chunk in client.get_streaming_response("Hello", tools=tools):
349+
# Chat client streaming - tools go in options dict
350+
async for chunk in client.get_streaming_response(
351+
"Hello",
352+
options={"tools": tools}
353+
):
345354
if chunk.text:
346355
print(chunk.text, end="")
347356

348-
# Agent streaming
349-
async for chunk in agent.run_stream("Hello"):
357+
# Agent streaming - tools can be keyword arg on agents
358+
async for chunk in agent.run_stream("Hello", tools=tools):
350359
if chunk.text:
351360
print(chunk.text, end="", flush=True)
352361
```
353362

354-
Tip: In Agent Framework, both clients and agents yield the same update shape; you can read `chunk.text` in either case.
363+
Tip: In Agent Framework, both clients and agents yield the same update shape; you can read `chunk.text` in either case. Note that for chat clients, `tools` goes in the `options` dict, while for agents, `tools` remains a direct keyword argument.
355364

356365
### Message Types and Creation
357366

@@ -1345,7 +1354,7 @@ Agent Framework provides built-in request-response capabilities where any execut
13451354

13461355
```python
13471356
from agent_framework import (
1348-
RequestInfoEvent, WorkflowBuilder, WorkflowContext,
1357+
RequestInfoEvent, WorkflowBuilder, WorkflowContext,
13491358
Executor, handler, response_handler
13501359
)
13511360
from dataclasses import dataclass
@@ -1361,7 +1370,7 @@ class ApprovalRequest:
13611370

13621371
# Workflow executor that requests human approval
13631372
class ReviewerExecutor(Executor):
1364-
1373+
13651374
@handler
13661375
async def review_content(
13671376
self,
@@ -1374,7 +1383,7 @@ class ReviewerExecutor(Executor):
13741383
agent_name="writer_agent"
13751384
)
13761385
await ctx.request_info(request_data=approval_request, response_type=str)
1377-
1386+
13781387
@response_handler
13791388
async def handle_approval_response(
13801389
self,

agent-framework/migration-guide/from-semantic-kernel/index.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,18 +769,37 @@ arguments = KernelArguments(settings)
769769
response = await agent.get_response(user_input, thread=thread, arguments=arguments)
770770
```
771771

772-
**Solution**: Simplified options in Agent Framework
772+
**Solution**: Simplified TypedDict-based options in Agent Framework
773773

774-
Agent Framework allows the passing of all parameters directly to the relevant methods, so that you don't have to import anything extra, or create any options objects, unless you want to. Internally, it uses a `ChatOptions` object for `ChatClients` and `ChatAgents`, which you can also create and pass in if you want to. This is also created in a `ChatAgent` to hold the options and can be overridden per call.
774+
Agent Framework uses a TypedDict-based options system for `ChatClients` and `ChatAgents`. Options are passed via a single `options` parameter as a typed dictionary, with provider-specific TypedDict classes (like `OpenAIChatOptions`) for full IDE autocomplete and type checking.
775775

776776
```python
777-
agent = ...
777+
from agent_framework.openai import OpenAIChatClient
778+
779+
client = OpenAIChatClient()
778780

779-
response = await agent.run(user_input, thread, max_tokens=1000, frequency_penalty=0.5)
781+
# Set default options at agent creation
782+
agent = client.create_agent(
783+
instructions="You are a helpful assistant.",
784+
default_options={
785+
"max_tokens": 1000,
786+
"temperature": 0.7,
787+
}
788+
)
789+
790+
# Override options per call
791+
response = await agent.run(
792+
user_input,
793+
thread,
794+
options={
795+
"max_tokens": 500,
796+
"frequency_penalty": 0.5,
797+
}
798+
)
780799
```
781800

782801
> [!NOTE]
783-
> The above is specific to a `ChatAgent`, because other agents might have different options, they should all accepts `messages` as a parameter, since that is defined in the `AgentProtocol`.
802+
> The `tools` and `instructions` parameters remain as direct keyword arguments on agent creation and `run()` methods, and are not passed via the `options` dictionary. See the [Typed Options Upgrade Guide](../../support/upgrade/typed-options-guide-python.md) for detailed migration patterns.
784803
785804
::: zone-end
786805

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
- name: Upgrade Guide - Workflow APIs and Request-Response System in Python
2-
href: requests-and-responses-upgrade-guide-python.md
2+
href: requests-and-responses-upgrade-guide-python.md
3+
- name: Upgrade Guide - Python Options based on TypedDicts
4+
href: typed-options-guide-python.md

0 commit comments

Comments
 (0)