.NET: Python: Merge main into feature-durabletask branch (#3385)

* Python: Add factory pattern to concurrent orchestration builder (#2738)

* Add factory pattern to concurrent orchestration builder

* Update readme

* Address AI comments

* Fix unit tests

* Fix import

* Prevent multiple calls to set participants or factories

* Add comments

* Mitigate warnings

* Fix mypy

* Address comments

* Address Copilot comments

* Fix tests

* Python: fix: GroupChat ManagerSelectionResponse JSON Schema for OpenAI Structured Outpu… (#2750)

* fix: ManagerSelectionResponse JSON Schema for OpenAI Structured Output Strict Mode

* refactor: install pre-commit then commit again

* Capture file IDs from code interpreter in streaming responses (#2741)

* .NET: [BREAKING] Prevent nulls in AIAgent property (#2719)

* prevent nulls in AIAgent property

* address feedback

* code ql sm04598 (#2723)

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>

* .NET: Add Conversation State Sample (Step05) (#2697)

* Initial plan

* Add Agent_OpenAI_Step05_Conversation sample for conversation state management

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Update Program.cs comment to accurately describe the sample

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Update the code to use the ConversationClient more in line with the samples in OpenAI

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Changing sample to use ChatClientAgent and conversationId in GetNewThread

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Bump AWSSDK.Extensions.Bedrock.MEAI from 4.0.4.7 to 4.0.4.11 (#2777)

---
updated-dependencies:
- dependency-name: AWSSDK.Extensions.Bedrock.MEAI
  dependency-version: 4.0.4.11
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Azure.Identity from 1.17.0 to 1.17.1 (#2780)

---
updated-dependencies:
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Azure.AI.AgentServer.AgentFramework from 1.0.0-beta.4 to 1.0.0-beta.5 (#2778)

---
updated-dependencies:
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Python: added more complete parsing for mcp tool arguments (#2756)

* added more complete parsing for mcp tool arguments

* fixed mypy

* added nonlocal model counter, and some fixes

* fixes in naming logic

* extracted json parsing function, added parametrized test and checked coverage

* Python: Updated package versions (#2784)

* Updated package versions

* Small fix

* Bump actions/checkout from 5 to 6 (#2404)

Bumps [actions/checkout](https://github.com/actions/checkout) from 5 to 6.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* .NET: adds support for labels in edges,  fixes rendering of labels in dot a… (#1507)

* adds support for labels in edges,  fixes rendering of labels in dot and mermaid, adds rendering of labels in edges

* Update dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* escaping edge labels, adding tests for labels containing strange characters that would break the diagram and enabling the previous signature so the API has backwards compatibility.

* Unify label in EdgeData

* Edge API adjustments, removed useless "sanitizer"

* fixed test

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Jacob Alber <jaalber@microsoft.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Python: Added custom args and thread object to ai_function kwargs (#2769)

* Added an example of using kwargs in ai_function

* Added thread object to ai_function kwargs

* Updated docs

* Small fix

* Added thread parameter filtering

* Fix WorkflowAgent to include thread convo history. Enable checkpointing. (#2774)

* Update OpenAIResponses.yaml to match AgentSchema (#2598)

1. Update `connection` child types --  `kind: ApiKey` to `kind: key` otherwise schema will fail: https://microsoft.github.io/AgentSchema/reference/apikeyconnection/

2.  Update `outputSchema`'s `PropertySchema` to be `kind` instead of `type` otherwise schema will fail: https://microsoft.github.io/AgentSchema/reference/propertyschema/

* Python: Remove warnings from workflow builder on not using factories (#2808)

* Revert concurrent

* Fix comments

* Python: Filter framework kwargs from MCP tool invocations (#2870)

* Filter framework kwargs from MCP tool invocations

* Fixes

* Python: Fix WorkflowAgent to emit yield_output as agent response (#2866)

* Fix WorkflowAgent to emit yield_output as agent response

* use raw_representation

* Raw representation handling

* Python: Use agent description in HandoffBuilder auto-generated tools (#2713) (#2714)

## Summary
Enhanced `HandoffBuilder._apply_auto_tools` to use the target agent's
description when creating handoff tools, providing more informative tool
descriptions for LLMs.

## Changes
- Modified `_apply_auto_tools` to extract `description` from
  `AgentExecutor._agent` when available
- Updated iteration to use `.items()` for more efficient dict traversal
- Handoff tools now use agent descriptions instead of generic placeholders

## Example
Before: "Handoff to the refund_agent agent."
After: "You handle refund requests. Ask for order details and process refunds."

## Testing
- All handoff tests pass (20/20)
- No breaking changes to existing API

Fixes #2713

Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>

* Python: [BREAKING] Observability updates (#2782)

* fixes Python: Add env_file_path parameter to setup_observability() similar to AzureOpenAIChatClient
Fixes #2186

* WIP on updates using configure_azure_monitor

* improved setup and clarity

* fixed root .env.example

* revert changes

* updated files

* updated sample

* updated zero code

* test fixes and fixed links

* fix devui

* removed planning docs

* added enable method and updated readme and samples

* clarified docstring

* add return annotation

* updated naming

* update capatilized version

* updated readme and some fixes

* updated decorator name inline with the rest

* feedback from comments addressed

* Python: Fix middleware terminate flag to exit function calling loop immediately (#2868)

* Fix middleware terminate flag to exit function calling loop immediately

* Eliminating duck typing

* Improve function exec result handling

* Fix race condition

* Fix mypy issues

* Python: Fix context duplication in handoff workflows when restoring from checkpoint (#2867)

* Fix context duplication in handoff workflows when restoring from checkpoint

* Address Copilot PR review

* .NET: Update to latest Azure.AI.*, OpenAI, and M.E.AI* (#2850)

* Update to latest Azure.AI.*, OpenAI, and M.E.AI*

Absorb breaking changes in Responses surface area

* Update dotnet/samples/AgentWebChat/AgentWebChat.AgentHost/Utilities/ChatClientExtensions.cs

* Update dotnet/samples/AgentWebChat/AgentWebChat.AgentHost/Utilities/ChatClientExtensions.cs

* Update dotnet/samples/AgentWebChat/AgentWebChat.AgentHost/Utilities/ChatClientExtensions.cs

* Update dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Using patch to remove the model is necessary, updated the response client to actually use the the ForAgent

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>

* Bump actions/download-artifact from 6 to 7 (#2862)

Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 6 to 7.
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](https://github.com/actions/download-artifact/compare/v6...v7)

---
updated-dependencies:
- dependency-name: actions/download-artifact
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump actions/cache from 4 to 5 (#2861)

Bumps [actions/cache](https://github.com/actions/cache) from 4 to 5.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump actions/upload-artifact from 5 to 6 (#2860)

Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 5 to 6.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/upload-artifact
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Python : Ollama Connector for Agent Framework (#1104)

* Initial Commit for Olama Connector

* Added Olama Sample

* Add Sample & Fixed Open Telemetry

* Fixed Spelling from Olama to Ollama

* remove"opentelemetry-semantic-conventions-ai ~=0.4.13" since its handled in a different pr

* Added Tool Calling

* Finalizing test cases

* Adjust samples to be more reliable

* Update python/packages/ollama/agent_framework_ollama/_chat_client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/packages/ollama/pyproject.toml

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/packages/ollama/tests/test_ollama_chat_client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/packages/ollama/agent_framework_ollama/_chat_client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Improved Docstrings & Sample

* Update python/packages/ollama/agent_framework_ollama/_chat_client.py

Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>

* Integrate PR Feedback
- Divided Streaming and Non-Streaming into independent Methods
- Catch Ollama Validation Error
- Add OTEL Provider Name
- Checked Ollama Messages
- Add Usage Statistics

* Revert setting, so it can be none

* Validate Message formatting between AF and Ollama

* Catch Ollama Error and raise a ServiceResponse Error

* Fix mypy error

* remove .vscode comma

* Add Reasoning support & adjust to new structure

* Add Ollama Multimodality and Reasoning

* Add test cases for reasoning

* Add Tests for Error Handling in Ollama Client

* Update python/samples/getting_started/multimodal_input/ollama_chat_multimodal.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Integrated Copilot Feedback

* Implement first PR Feedback

* Adjust Readme files for examples

* Adjust argument passing via additional chat options

* Implemented PR Feedback

* Removing Ollama Package from Core and moving samples

* Fix Link & Adding Samples to Main Sample Readme

* Fixing Links in Readme

* Moved Multimodal and Chat Example

* Fixed Link in ChatClient to Ollama

* Fix AgentFramework Links in Ollama Project

* Fix observability breaking change

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>

* Skip failing IT (#2904)

* .NET: Cosmos DB UT Fast Skip (For Non-Configured Local envs) (#2906)

* Cosmos DB UT Fast Skip (Non-Configured Local envs) + Long running UT skip in pipeline when no CosmosDB changes happened

* Force a CosmosDB source code change to trigger the pipeline

* Address possible string boolean mismatch

* Add debug

* Enabling emulator always when running IT

* .NET: Add TTLs to durable agent sessions (#2679)

* .NET: Add TTLs to durable agent sessions

* Remove unnecessary async

* PR feedback: clarify UTC

* PR feedback: limit minimum signal delay to <= 5 minutes

* PR feedback: Fix TTL disablement

* Linter: use auto-property

* Fix build break from OpenAI SDK change

* Updated CHANGELOG.md

* PR feedback

* Reduce default TTL to 14 days to work around DTS bug

* Python:  Update Mem0Provider to use v2 search API `filters` parameter (#2766)

* short fix to move id parameters to filters object

* added tests

* small fix

* mem0 dependency update

* Updated package versions (#2913)

* .NET: Switch to new "Run" method name. (#2843)

* Switch to new "RunAgent" method name.

* Try to disable false positive naming warning.

* Add comment about disabled warnings.

* Rename `RunAgent` to just `Run`.

* Update CHANGELOG.

* Python: Switch to new "run" method name. (#2890)

* Switch to `run` method.

* Add support for deprecated `run_agent`.

* Fix entity method name.

* Fix method name and improve tests.

* Update comment.

* Update Python CHANGELOG.

* [BREAKING] Python: Add factory pattern to handoff orchestration builder (#2844)

* WIP: Factory pattern to handoff

* Add factory pattern to concurrent orchestration builder; Next: tests and sample verification

* Add tests and improve comments

* Fix mypy

* Simplify handoff_simple.py

* Simplify handoff_autonoumous.py and bug fix

* Update readme

* Address Copilot comments

* Python: Flow custom kwargs to agents via Workflow SharedState (#2894)

* Flow custom kwargs to agents via SharedState

* Address Copilot feedback

* Improve sample typing

* Fix test

* Fix Pydantic error when using Literal type for tool params (#2893)

* Updated Ollama package version (#2920)

* Python: Azure AI Agent with Bing Grounding Citations Sample (#2892)

* bing grounding sample with citations

* small fix

* fix

* .NET: Make DelegatingAIAgent abstract (#2797)

* Initial plan

* Make DelegatingAIAgent abstract

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Added additional arguments for Azure AI agent (#2922)

* Python: Correction of MCP image type conversion in  _mcp.py (#2901)

* Correction of MCP image type conversion in  _mcp.py

* Added a new overload to the init function of the DataContent() type of the Agent Framework, edited the test case to correctly test the usage of the data and uri fields while using DataContent()

* Fixed tests related to the changes of the DataContent type, added testing for both string and byte representations

* Pass kwargs into subworkflows (#2923)

* Python: Move ollama samples to samples getting started dir (#2921)

* Move ollama samples to samples getting started dir

* Address feedback

* Python: fix: correct BadRequestError when using Pydantic model in response_fo… (#1843)

* fix: correct BadRequestError when using Pydantic model in response_format

* Fix lint

---------

Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>

* .NET: [Breaking] Delete display name property (#2758)

* delete the AIAgent.DisplayName property

* use agent name as a first value for activity display name

* Update dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Python: cleanup and refactoring of chat clients (#2937)

* refactoring and unifying naming schemes of internal methods of chat clients

* set tool_choice to auto

* fix for mypy

* added note on naming and fix #2951

* fix responses

* fixes in azure ai agents client

* Python: Workflow add option to visualize internal executors (#2917)

* Workflow add option to visualize internal executors

* Address Copilot comments

* Python: Fixes Run ID and Thread ID casing to align with AG-UI Typescript SDK (#2948)

* added camelCase input to run id and thread id aligning with @ag-ui/core

* fixed per copilot suggestions

* Python: Add workflow cancellation sample (#2732)

* Add workflow cancellation sample

Add sample demonstrating how to cancel a running workflow using asyncio
tasks. Shows both cancellation mid-execution and normal completion paths.
Useful for implementing timeouts, graceful shutdown, or A2A executors.

* update docstring

* .NET: Update Anthropic package to version 12.0.0 (#2914)

* Initial plan

* Update Anthropic package to version 12.0.0

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

* Python: Add Azure Managed Redis Support with Credential Provider (#2887)

* azure redis support

* small fixes

* azure managed redis sample

* fixes

* Bump CommunityToolkit.Aspire.OllamaSharp from 13.0.0-beta.440 to 13.0.0 (#2856)

---
updated-dependencies:
- dependency-name: CommunityToolkit.Aspire.OllamaSharp
  dependency-version: 13.0.0
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump AWSSDK.Extensions.Bedrock.MEAI from 4.0.4.11 to 4.0.5 (#2853)

---
updated-dependencies:
- dependency-name: AWSSDK.Extensions.Bedrock.MEAI
  dependency-version: 4.0.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>

* Bump Azure.AI.AgentServer.AgentFramework from 1.0.0-beta.4 to 1.0.0-beta.5 (#2854)

---
updated-dependencies:
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Python: Fix WorkflowAgent event handling and kwargs forwarding (#2946)

* Fix kwargs propagation through workflow.as_agent()

* Fix WorkflowAgent to respect AgentExecutor output_response setting

* .NET: Use GrpcEntityRunner instead of TaskEntityDispatcher (#2759)

* Use GrpcEntityRunner instead of TaskEntityDispatcher

* Pin to Durable worker 1.11.0

* Set the invocation result

* Update all Durable packages

* Update changelog, rename dispatcher to encondedEntityRequest

* Python: Bump Py version to 1.0.0b251218 for a release. Update CHANGELOG (#2968)

* Bump Py version to 1.0.0b251218 for a release. Update CHANGELOG

* update lock

* Fix formatting

* Fix ChatKit typing

* Python: Introducing Foundry Local Chat Clients (#2915)

* redo foundry local chat client

* fix mypy and spelling

* better docstring, updated sample

* fixed tests and added tests

* small sample update

* Updated package versions (#2978)

* Python: Added GitHub MCP sample with PAT (#2967)

* added github mcp sample with PAT

* addressed copilot fixes

* env fix

* Python: Preserve reasoning blocks with OpenRouter (#2950)

* Preserve reasoning blocks with OpenRouter

* Put encrypted reasoning in TextReasoningContent

* Remove unneccessary change

* Fix docs

* Support streaming

* Fix handling None in TextReasoningContent.text

* Python: Added response.created and response.in_progress event process to OpenAIBaseResponseClient (#2975)

* added response.created and response.in_progress to include response.id

* better doc string

* added tests for the new streaming event types

* Python: Introducing support for Bedrock-hosted models (Anthropic, Cohere, etc.) (#2610)

* Pushing the bedrock related changes to the new branch after addressing the review comments

* 2524 Addressed the second round review comments

* 2524 Addressed few more minor comments on the PR

* resolving the merge conflict

* 2524 resolved the uv.lock conflicts

* 2524 addressed more comments

* 2524 removed the print statement to fix the checks failure

* 2524 resolved the CI failure issues

* 2524 fixing the CI breaks

* 2524 Addressed the review comment

* 2524 resolved conflict

---------

Co-authored-by: Sunil Dutta <sunil.dutta@penske.com>
Co-authored-by: budgetboardingai <apurva.sharma31@gmail.com>

* .NET: [Durable Agents] Reliable streaming sample (#2942)

* .NET: [Durable Agents] Reliable streaming sample

* Add automated validation for new sample

* Address Copilot PR feedback

* Fix typo in README.md about agent definitions (#2634)

* Fix typo in README.md about agent definitions

* Update agent-samples/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Python: latency improvements (#3014)

* latency improvements

* fixed mypy, added coding standards and instructions

* slight logic improvement

* Python: Updated package versions (#3024)

* Updated package versions

* Updated changelog

* Python: add powerfx safe mode (#3028)

* add powerfx safe mode

* improved docstring and aligned env_file loading

* ensured test uses reset

* .NET: [Breaking] Introduce RunCoreAsync/RunCoreStreamingAsync delegation pattern in AIAgent (#2749)

* Initial plan

* Refactor AIAgent: Make RunAsync and RunStreamingAsync non-abstract, add RunCoreAsync and RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix infinite recursion in test implementations

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Make RunAsync and RunStreamingAsync non-virtual as requested

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix DelegatingAIAgent subclasses to use RunCoreAsync/RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix XML documentation references in AnonymousDelegatingAIAgent

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Restore <see cref> tags with proper qualified signatures in AnonymousDelegatingAIAgent

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Rollback unnecessary XML documentation changes in AnonymousDelegatingAIAgent

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Remove pragma and update crefs to RunCoreAsync/RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix EntityAgentWrapper to call base.RunCoreAsync/RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* fix compilation issues

* fix compilatio issue

* fix tests

* fix unit tests

* fix unit test

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <sergemenshikh@gmail.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* add issue template and additional labeling (#3006)

* fix and extra int test (#3037)

* .NET: [BREAKING] Refactor ChatMessageStore methods to be similar to AIContextProvider and add filtering support (#2604)

* Refactor ChatMessageStore methods to be similar to AIContextProvider

* Fix file encoding

* Ensure that AIContextProvider messages area also persisted.

* Update formatting and seal context classes

* Improve formatting

* Remove optional messages from constructor and add unit test

* Add ChatMessageStore filtering via a decorator

* Update sample and cosmos message store to store AIContextProvider messages in right order. Fix unit tests.

* Update Workflowmessage store to use aicontext provider messages.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Improve xml docs messaging

* Address code review comments.

* Also notify message store on failure

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* [BREAKING] Remove unused AgentThreadMetadata (#3067)

* Remove unused AgentThreadMetadata

* Update DurableTask Changelog

* Python: Fix AzureAIClient failure when conversation history contains assistant messages (#3076)

* Fix AzureAIClient failure when conversation history contains assistant messages

* Address PR review feedback: improve docstring and test assertions

* Remove redundant cast

* Fix: Update OTLP exporter protocol conditions (#3070)

* Python: Fix ExecutorInvokedEvent and ExecutorCompletedEvent observability data (#3090)

* Fix ExecutorInvokedEvent.data mutation bug

* Fix bug related to not yielding output type

* .NET: Seal ChatClientAgentThread (#2842)

* Initial plan

* Seal ChatClientAgentThread class

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix broken strands urls. (#3102)

* Fix broken strands urls.

* Fix typos

* .NET: Fix message ordering inconsistency when using AIContextProvider (#2659)

* Initial plan

* Fix message ordering inconsistency when using AIContextProvider

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Revert to original message ordering: Input, AIContextProvider, Response

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Reorder messages to ChatClient to match MessageStore order: Existing, Input, AIContextProvider

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Remove redundant test methods as existing tests already verify the behavior

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* fix: tool_choice parameter not being honored when passed to agent.run() (#3095)

* sharepoint sample fix (#3108)

* Bump versions to 1.0.0b260106 for a release. Update CHANGELOG.md (#3109)

* Bump Bedrock version to latest (#3110)

* Python: Fix MCP tool result serialization for list[TextContent] (#2523)

* Fix MCP tool result serialization for list[TextContent]

When MCP tools return results containing list[TextContent], they were
incorrectly serialized to object repr strings like:
'[<agent_framework._types.TextContent object at 0x...>]'

This fix properly extracts text content from list items by:
1. Checking if items have a 'text' attribute (TextContent)
2. Using model_dump() for items that support it
3. Falling back to str() for other types
4. Joining single items as plain text, multiple items as JSON array

Fixes #2509

* Address PR review feedback for MCP tool result serialization

- Extract serialize_content_result() to shared _utils.py
- Fix logic: use texts[0] instead of join for single item
- Add type annotation: texts: list[str] = []
- Return empty string for empty list instead of '[]'
- Move import json to file top level
- Add comprehensive unit tests for serialization

* Address PR review feedback: fix type checking and double serialization

- Add isinstance(item.text, str) check to ensure text attribute is a string
- Fix double-serialization issue by keeping model_dump results as dicts
  until final json.dumps (removes escaped JSON strings in arrays)
- Improve docstring with detailed return value documentation
- Add test for non-string text attribute handling
- Add tests for list type tool results in _events.py path

* Simplify PR: minimal changes to fix MCP tool result serialization

Addresses reviewer feedback about excessive refactoring:
- Reset _events.py to original structure
- Only add import and use serialize_content_result in one location
- All review comments addressed in serialize_content_result():
  - Added isinstance(item.text, str) check
  - Use model_dump(mode="json") to avoid double-serialization
  - Improved docstring with explicit return value documentation
  - Empty list returns "" instead of "[]"

* Refactor: Move MCP TextContent serialization to core prepare_function_call_results

Per reviewer feedback, moved the TextContent serialization logic from
ag-ui's serialize_content_result to the core package's
prepare_function_call_results function.

Changes:
- Added handling for objects with 'text' attribute (like MCP TextContent)
  in _prepare_function_call_results_as_dumpable
- Removed serialize_content_result from ag-ui/_utils.py
- Updated _events.py and _message_adapters.py to use
  prepare_function_call_results from core package
- Updated tests to match the core function's behavior

* Fix failing tests for prepare_function_call_results behavior

- test_tool_result_with_none: Update expected value to 'null' (JSON serialization of None)
- test_tool_result_with_model_dump_objects: Use Pydantic BaseModel instead of plain class

* Fix B903 linter error: Convert MockTextContent to dataclass

The ruff linter was reporting B903 (class could be dataclass or namedtuple)
for the MockTextContent test helper classes. This commit converts them to
dataclasses to satisfy the linter check.

* Python: Improve DevUI, add Context Inspector view as new tab under traces (#2742)

* Improve DevUI, add Context Inspector view as new tab under traces

* fix mypy errors

* fix: Handle stale MCP connections in DevUI executor

MCP tools can become stale when HTTP streaming responses end - the underlying
stdio streams close but `is_connected` remains True. This causes subsequent
requests to fail with `ClosedResourceError`.

Add `_ensure_mcp_connections()` to detect and reconnect stale MCP tools before
agent execution. This is a workaround for an upstream Agent Framework issue
where connection state isn't properly tracked.

Fixes MCP tools failing on second HTTP request in DevUI.

fixes  #1476 #1515 #2865

* fix #1572 report import dependency errors more clearly

* Ensure there is streaming toggle where users can select streaming vs non streaming mode in devui . Fixes .NET: [Python] DevUI tool call rendering in non-streaming mode?

* remove unused dead code

* improve ux - workflows with agents show a chat component in execution timelien, also ensure magentic final output shows correctly

* update ui build

* update devui to use instrumentation instead of tracing, other instrumentation and type/instance check fixes

* .NET: Seal factory contexts and add non JSO deserialize overloads (#3066)

* Seal factory contexts and add non JSO deserialize overloads

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Enable blank issues in issue template configuration

Need to re-enable creating blank issues

* updated templates (#3106)

* updated templates

* enabled blank and fixed triage

* made language optional and moved to the bottom for features

* Python: Streaming sample for azurefunctions (#3057)

* Streaming sample for azurefunctions

* Fixed links and sample name

* Addressed feedback

* Addressed feedback

* Fixed integration tests

* Updated test

* Python: fix(azure-ai): Fix response_format handling for structured outputs (#3114)

* fix(azure-ai): read response_format from chat_options instead of run_options

* refactor: use explicit None checks for response_format

* Fix mypy error

* Mypy fix

* Python: Bump python version to 1.0.0b260107 for a release (#3128)

* Bump python version to 1.0.0b260107 for a release

* Update changelog

* Make A2AAgent public, so that it's concrete implementation methods can be used. (#3119)

* .NET: Map additional props <-> A2A metadata (#3137)

* map additional props from agent run options to a2a request metadata

* small touches

* add unit tests for new extension methods

* Sort using

* add unit test

* add additiona unit tests

* special case json element to avoid unnecessary serialization

* Python: Fix Anthropic streaming response bugs (#3141)

* test commit identity

* fix(anthropic): fix raw_representation and finish_reason in streaming

* lint fix

* Bump AWSSDK.Extensions.Bedrock.MEAI from 4.0.5 to 4.0.5.1 (#2994)

---
updated-dependencies:
- dependency-name: AWSSDK.Extensions.Bedrock.MEAI
  dependency-version: 4.0.5.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Bump Anthropic from 12.0.0 to 12.0.1 (#2993)

---
updated-dependencies:
- dependency-name: Anthropic
  dependency-version: 12.0.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* .NET: [Breaking] Prevent loss of input messages & streamed updates when resuming streaming (#2748)

* save input messages and stream updates to the continuation token to be able to use them in the last successful stream resumption call.

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_BackgroundResponsesTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix typo

* init continuation token from chat response

* remove unnecessary types for source generation

* remove check for continuation token passed at initial run

* remove check for continuation token pass at initial run

* centralize continuation token parsing

* update xml comments

* use readonly collection instead of enumerable

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* .NET: fix: Expose WorkflowErrorEvent as ErrorContent (#2762)

* fix: Expose WorkflowErrorEvent as ErrorContent

When hosted using .AsAgent(), Workflows were not exposing inner errors coming as Exceptions (through the WorkflowErrorEvent)

The fix is to convert their message to an ErrorContent on the way out, rather than rely on the default "empty update" to collect the raw event.

* feat: Add a way to show/suppress exception information

* Bump Microsoft.Agents.AI.Workflows from 1.0.0-preview.251125.1 to 1.0.0-preview.251219.1 (#2997)

---
updated-dependencies:
- dependency-name: Microsoft.Agents.AI.Workflows
  dependency-version: 1.0.0-preview.251219.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* .NET: Add Run overloads to expose ChatClientAgentRunOptions in IntelliSense (#3115)

* Initial plan

* Add ChatClientAgentExtensions for improved discoverability of ChatClientAgentRunOptions

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Address code review feedback - use collection expression syntax

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Apply suggestion from @westey-m

* Fix issues with Copilot implementation

* Add additional tests for structured output overloads.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Python: Add tool call/result content types and update connectors and samples (#2971)

* Add new AI content types and image tool support

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Add Python content types for tool calls/results and image generation tool support

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Address review feedback for tool content and samples

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Tighten image generation typing and sample tools list

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Align image generation output typing

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Handle MCP naming, image options mapping, and connector tool content

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Allow MCP call in function approval request

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Remove raw image_generation tool remapping

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Restore Anthropic tool_use to function calls unless code execution

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix lint issues for hosted file docstring and MCP parsing

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Import ChatResponse types in Anthropic client

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix Anthropics citation type imports and MCP typing for handoff/tools

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Skip lightning tests without agentlightning and fix function call import

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* fix lint on lab package

* rebuilt anthropic parsing

* redid anthropic parsing

* typo

* updated parsing and added missing docstrings

* fix tests

* mypy fixes

* second mypy fix

* add new class to other samples

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>

* Bump Google.GenAI from 0.6.0 to 0.9.0 (#2995)

---
updated-dependencies:
- dependency-name: Google.GenAI
  dependency-version: 0.9.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Bump js-yaml from 4.1.0 to 4.1.1 in /python/packages/devui/frontend (#3123)

Bumps [js-yaml](https://github.com/nodeca/js-yaml) from 4.1.0 to 4.1.1.
- [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md)
- [Commits](https://github.com/nodeca/js-yaml/compare/4.1.0...4.1.1)

---
updated-dependencies:
- dependency-name: js-yaml
  dependency-version: 4.1.1
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Updated package versions (#3144)

* .NET: Bump Microsoft.Agents.AI.OpenAI and Microsoft.Extensions.AI.OpenAI (#2996)

* Bump Microsoft.Agents.AI.OpenAI and Microsoft.Extensions.AI.OpenAI

Bumps Microsoft.Agents.AI.OpenAI from 1.0.0-preview.251125.1 to 1.0.0-preview.251219.1
Bumps Microsoft.Extensions.AI.OpenAI from 10.1.0-preview.1.25608.1 to 10.1.1-preview.1.25612.2

---
updated-dependencies:
- dependency-name: Microsoft.Agents.AI.OpenAI
  dependency-version: 1.0.0-preview.251219.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Microsoft.Extensions.AI.OpenAI
  dependency-version: 10.1.1-preview.1.25612.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Microsoft.Agents.AI.OpenAI
  dependency-version: 1.0.0-preview.251219.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Microsoft.Extensions.AI.OpenAI
  dependency-version: 10.1.1-preview.1.25612.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Fixed samples

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>

* Python: fix(ag-ui): Execute tools with approval_mode, fix shared state, code cleanup  (#3079)

* fix(ag-ui): execute tools after approval in human-in-the-loop flow

* Fix shared state bug

* Bug fix finalized

* Refactoring to clean up code

* Code cleanup

* More fixes

* More code cleanup

* Add version detection in __init__.py to ruff ignore list

* Track agent name with updates for workflow agent (#3146)

* Python: Fix AzureAIClient tool call bug for AG-UI use (#3148)

* Fiz AzureAIClient tool call bug

* Address copilot feedback

* Python: multiple bug fixes (#3150)

* fix Python: kwargs are not passed to _prepare_thread_and_messages in ChatAgent.run
Fixes #3118

* fix Python: [Bug]: model_id versus model_deployment_name is confusing in Azure AI Agents
Fixes #3147

* add types

* fixed type and docstring

* fix(anthropic): fix duplicate ToolCallStartEvent in streaming tool calls (#3051)

When processing `input_json_delta` events, the Anthropic client was
passing the tool name from the previous `tool_use` event. This caused
ag-ui's `_handle_function_call_content` to emit a `ToolCallStartEvent`
for every streaming chunk (since it triggers on `if content.name:`).

This fix changes the behavior to pass an empty string for `name` in
`input_json_delta` events, matching OpenAI's behavior where streaming
argument chunks have `name=""`. The initial `tool_use` event still
provides the tool name, so only one `ToolCallStartEvent` is emitted.

Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>

* .NET: [BREAKING] Change GetNewThread and DeserializeThread to async (#3152)

* Change GetNewThread and DeserializeThread plus ChatMessageStore and AIContextProvider Factories to async

* Merge fixes

* Fix Ollama model env var in documentation (#3156)

Signed-off-by: Dina Suehiro Jones <dina.s.jones@intel.com>

* Python: Add Pydantic request model and OpenAPI tags support to AG-UI FastAPI endpoint (#2522)

* feat(ag-ui): Add Pydantic request model and OpenAPI tags support

- Add AGUIRequest Pydantic model in _types.py with field descriptions
- Update add_agent_framework_fastapi_endpoint() to accept tags parameter
- Use AGUIRequest model for automatic validation and OpenAPI schema generation
- Export AGUIRequest and DEFAULT_TAGS in __init__.py
- Update test_endpoint.py to expect 422 for invalid requests
- Add tests for OpenAPI schema, default tags, custom tags, and validation

Benefits:
- Better API documentation with complete request schema in Swagger UI
- Automatic request validation with Pydantic
- Organized endpoints under 'AG-UI' tag instead of 'default'
- Improved developer experience and type safety

Fixes #<issue-number>

* test(ag-ui): Add test for internal error handling to achieve 100% coverage

- Add test_endpoint_internal_error_handling() to cover exception handling code
- Mock copy.deepcopy to simulate internal error during default_state processing
- Add type: ignore for FastAPI tags parameter (known pyright compatibility issue)
- Achieves 100% test coverage for _endpoint.py (previously missing lines 103-105)

* .NET: Improve resolving `AITool` from DI (#3175)

* remove localagenttoolregistry

* also give the factory method API

* Python: Fix MCPStreamableHTTPTool to use new streamable_http_client API (#3088)

* Fix MCPStreamableHTTPTool to use new streamable_http_client API with proper httpx client cleanup

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Update docstring to reflect new streamable_http_client API usage

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Refactor MCPStreamableHTTPTool to accept optional http_client parameter and delegate client creation to streamable_http_client

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Update mcp package minimum version to 1.24.0 for streamable_http_client API support

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix critical bugs: apply headers/timeout/sse_read_timeout when creating httpx client, add version constraint <2, and properly manage client lifecycle

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Simplify implementation: remove headers/timeout/sse_read_timeout params, remove kwargs, remove close() override per feedback

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Add back **kwargs parameter for backward compatibility (accepted but not used)

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Remove unused httpx import from test file

Note: The uv.lock file needs to be updated with 'uv sync' to reflect the mcp version constraint change (>=1.24.0,<2)

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* cicd fixes

* udpated samples with headers examples

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>

* azureai direct a2a endpoint support (#3127)

* Python: [BREAKING]: removed display_name, renamed context_providers, middleware and AggregateContextProvider (#3139)

* removed display_name, renamed context_providers, middleware and AggregateContextProvider

* fixes

* fixed test

* testfix

* removed mistakenly put back test

* updated new test

* rename middlewares to middleware

* middleware fixes

* Python: MCP Improvements: improved connection loss behavior, pagination for loading and a param to control representation (#3154)

* pagination support (#2848) added a parse_tool_result param and connection loss (#2884)

* fix #3153

* improved connection handling

* improved logic

* Python: Add declarative workflow runtime (#2815)

* Further support for declarative python workflows

* Add tests. Clean up for typing and formatting

* Improvements and cleanup

* Typing cleanup. Improve docstrings

* Proper code in docstrings

* Fix malformed code-block directive in docstring

* Remove dead links

* PR feedback

* Address PR feedback

* Address PR feedback

* Remove sl

* Update devui frontend

* More cleanup

* Fix uv lock

* Skip Py 3.14 tests as powerfx doesn't support it

* Fix mypy error

* Fix for tool calls

* Removed stale docstring

* Fix lint

* Standardize on .NET namespaces. Revert DevUI changes (bring in later)

* Implement remaining items for Python declarative support to match dotnet

* point URL to agent, not to agentcard (#3176)

* Python: [BREAKING]: Introducing Options as TypedDict and Generic (#3140)

* WIP typeddict for options

* updated all clients and ChatAgents

* updated everything

* added ADR

* fix mypy

* proper typevar imports

* fixed import

* fixed other imports

* slight update in the sample

* updated from feedback

* fixes

* fixed missing covariants and test fixes

* fixed typing

* updated anthropic thinking config

* ruff fixes

* fixed int tests

* fix tests and mypy

* updated integration tests

* updated docstring and test fix

* improved options handling in obser

* mypy fix

* updated a host of integration tests

* fix tests

* bedrock fix

* [BREAKING] Python: Refactor orchestrations (#3023)

* Group chat refactoring Part 1; Next: HIL and handoff

* Add agent approval flow; next samples

* WIP: samples

* WIP: HIL samples

* Group chat HIL working; next: handoff

* Fix group chat tool approval sample

* WIP: refactor handoff; next handoff handling

* Handoff done; next handoff samples and concurrent and sequential

* Handoff samples, concurrent, and sequential done; next Magentic

* WIP: magentic; next test with samples + HIL

* Magentic Working; next fix all samples and tests

* Fix handoff samples; next tests

* WIP: fixing tests; some orchestration as agent samples are failing

* Group chat unit tests done

* Handoff  unit tests done

* Remove old orchestration_request_info and fix related tests

* Magentic unit tests done

* Fix samples

* Fix test

* Fix test 2

* mypy

* Address comments

* Update readme

* Address comments

* Address comments 2

* Replace display name

* Python: ADR for create/get agent API (#2618)

* ADR for create/get agent API

* Updated ADR with implementation options

* Small updates

* Updated decision outcome section

* Updated broken links

* Small updates

* Fixed merge conflicts

* Small fix

* Updated decision outcome section

* Small fixes

* Updated provider naming based on client SDK

* Add ignored parameter for CodeQL in workflow (#3204)

* Implement IReadOnlyList on InMemoryChatMessageStore (#3205)

* .NET: Make ChatMessageStore and AIContextProvider context props settable (#3196)

* Make ChatMessageStore and AIContextProvider context props setable

* Add validation to preserve non-null requirement of certain properties.

* Fix broken tests.

* Python: Add dependencies param to ag-ui FastAPI endpoint (#3191)

* Add dependencies param to ag-ui FastAPI endpoint

* Address Copilot feedback

* renamed all (#3207)

* Python: ADR for simplified get response (#3098)

* ADR for simplified get response

* updated some language, added agent option and code comparison

* small update in sample

* added workflows and expanded some points

* changed decision and number

* updated with stream=False default

* .NET: [Breaking] Rename`AgentRunResponse` and `AgentRunResponseUpdate` classes (#3197)

* rename AgentRunResponse and AgentRunResponseUpdate classes - part1

* rename varialbles, parameters, methods and tests

* rollback unnecessary changes

* .NET: [Breaking] Rename AgentRunResponseEvent and AgentRunUpdateEvent classes (#3214)

* rename AgentRunResponseEvent and AgentRunUpdateEvent classes

* rollback unnecessary changes

* Python: Create/Get Agent API for Azure V2 (#3059)

* Added get_agent method to Azure AI V2

* Small fixes

* Small fix

* Removed AzureAIAgentProvider

* Added create_agent method

* Small fixes

* Fixed code interpreter tool mapping

* Added agent provider for V2 client

* Updated response format handling

* Added provider example

* Fixed errors

* Update python/samples/getting_started/agents/azure_ai/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Small fix

* Updates from merge

* Resolved comments

* Resolved comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Python: Add more specific exceptions to Workflow (#3188)

* Add more specifc workflow exceptions

* Fix tests

* AI comments

* Misc

* Python: Added AzureAI sample for downloading code interpreter generated files (#3189)

* added azure ai code interpreter file download sample

* copilot fix suggestions

* function name fixes + readme update

* small fix

* update package versions (#3223)

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>

* Python: fix(core): correct FunctionResultContent ordering in WorkflowAgent.merge_updates (#3168)

* fix(core): simplify FunctionResultContent ordering in WorkflowAgent.merge_updates

* improve comment

* Fix name

* fix(workflows): rename WorkflowOutputEvent.source_executor_id to executor_id for API consistency (#3166)

* Python: fix(ag-ui): add MCP tool support for AG-UI approval flows (#3212)

* add MCP tool support for AG-UI approval flows

* use attribute in place of property

* Python: Properly configure structured outputs based on new options dict (#3213)

* Properly configure structured outputs based on new options dict

* Fix mypy

* .NET: Merge AgentRunOptions.AdditionalProperties into ChatOptions.AdditionalProperties (#3184)

* Merge AgentRunOptions.AdditionalProperties into ChatOptions.AdditionalProperties

* Fix namespace and typo.

* .NET: Update Google.GenAI to 0.11.0 and remove polyfill implementations (#3232)

* Initial plan

* Update Google.GenAI to 0.11.0 and remove polyfill files

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* .NET: [BREAKING] Renamed CreateAIAgent/GetAIAgent to AsAIAgent (#3222)

* Renamed chat client extension method

* Additional renaming

* Updated documentation

* Fixed tests

* Small fix

* Small fix

* Updated DurableAIAgent and fixed integration tests (#3241)

* Python: Create/Get Agent API for Azure V1 (#3192)

* Added provider implementation for Azure AI V1

* Small fixes

* Fixed OpenAPI example

* Fixed local MCP example

* Fixed hosted MCP example

* Fixed file search sample

* Small fixes

* Resolved comments

* Doc updates

* Bump azure-core from 1.37.0 to 1.38.0 in /python (#3209)

Bumps [azure-core](https://github.com/Azure/azure-sdk-for-python) from 1.37.0 to 1.38.0.
- [Release notes](https://github.com/Azure/azure-sdk-for-python/releases)
- [Commits](https://github.com/Azure/azure-sdk-for-python/compare/azure-core_1.37.0...azure-core_1.38.0)

---
updated-dependencies:
- dependency-name: azure-core
  dependency-version: 1.38.0
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Python: Create/Get Agent API for OpenAI Assistants (#3208)

* Added provider implementation

* Added example with response format

* Small improvements

* Python: (AG-UI) Support service-managed thread on AG-UI  (#3136)

* added service thread support

* set service_thread_id to only supplied_thread_id

* uses raw_representation to extract the conversation_id

* removed accidental edit

* updated test to use raw_representation

* resolves copilot review feedback

* revert back StubAgent, since not used

* removed relative module import

* removed hasattr check per PR feedback

* Create/Get Agent API - fixes and example improvements (#3246)

* .NET Purview Middleware: Improve Background Job Runner Injection (#3256)

* Clean up background job dependency injection

* Fix xml documentation grammar

* Python: [BREAKING] Renamed create_agent to as_agent (#3249)

* Renamed create_agent to as_agent

* Override for as_agent

* Added override

* Python: Update package version (#3258)

* package version 260116

* removed name tags

* Python: Fixed Azure chat client for asynchronous filtering (#3260)

* Fixed Azure chat client for asynchronous filtering

* Updated test

* Python: Fixed use_agent_middleware calling private _normalize_messages (#3264)

* Fix use_agent_middleware calling private _normalize_messages

* Fixed A2A and Copilot Studio agent

* Python: Added rai_config to Azure AI agent creation (#3265)

* Add kwargs to create_agent method

* Added test for kwargs

* Addressed comment

* Added doc string

* Python: Filter conversation_id when passing kwargs to agent as tool (#3266)

* Filter conversation_id when passing kwargs to agent as tool

* Small fix

* Update python/samples/getting_started/agents/azure_ai/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/samples/getting_started/agents/openai/openai_responses_client_with_agent_as_tool.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/samples/getting_started/agents/azure_ai/azure_ai_with_agent_as_tool.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Bump actions/setup-dotnet from 5.0.1 to 5.1.0 (#3273)

Bumps [actions/setup-dotnet](https://github.com/actions/setup-dotnet) from 5.0.1 to 5.1.0.
- [Release notes](https://github.com/actions/setup-dotnet/releases)
- [Commits](https://github.com/actions/setup-dotnet/compare/v5.0.1...v5.1.0)

---
updated-dependencies:
- dependency-name: actions/setup-dotnet
  dependency-version: 5.1.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Update ignored checks in merge-gatekeeper workflow

* Python: [BREAKING] Make response_format validation errors visible to users (#3274)

* Make response_format validation errors visible to users

* Small fix

* Addressed comments

* Python: fix(declarative): Fix MCP tool connection not passed from YAML to Azure AI agent creation API (#3248)

* fix(declarative): Fix MCP tool connection not passed from YAML

* Add samples to README

* Fix mypy

* Fix mypy again

* Address PR comments

* fix #3171, ensure proper form rendering for int (#3201)

* Bump uv from 0.9.25 to 0.9.26 in /python (#3288)

Bumps [uv](https://github.com/astral-sh/uv) from 0.9.25 to 0.9.26.
- [Release notes](https://github.com/astral-sh/uv/releases)
- [Changelog](https://github.com/astral-sh/uv/blob/main/CHANGELOG.md)
- [Commits](https://github.com/astral-sh/uv/compare/0.9.25...0.9.26)

---
updated-dependencies:
- dependency-name: uv
  dependency-version: 0.9.26
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump ruff from 0.14.11 to 0.14.13 in /python (#3287)

Bumps [ruff](https://github.com/astral-sh/ruff) from 0.14.11 to 0.14.13.
- [Release notes](https://github.com/astral-sh/ruff/releases)
- [Changelog](https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md)
- [Commits](https://github.com/astral-sh/ruff/compare/0.14.11...0.14.13)

---
updated-dependencies:
- dependency-name: ruff
  dependency-version: 0.14.13
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump tar from 7.4.3 to 7.5.3 in /python/packages/devui/frontend (#3267)

Bumps [tar](https://github.com/isaacs/node-tar) from 7.4.3 to 7.5.3.
- [Release notes](https://github.com/isaacs/node-tar/releases)
- [Changelog](https://github.com/isaacs/node-tar/blob/main/CHANGELOG.md)
- [Commits](https://github.com/isaacs/node-tar/compare/v7.4.3...v7.5.3)

---
updated-dependencies:
- dependency-name: tar
  dependency-version: 7.5.3
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* .NET: Delete sync extension methods for agent (#3291)

* Delete sync extension methods for agent

* Fix comments and obsolete attribute

* Remove more sync methods.

* Fix naming and comments.

* Fix unit tests

* Python: Fix: Add system_instructions to ChatClient LLM span tracing (#3164)

* Fix: Add system_instructions to ChatClient LLM span tracing

- Add system_instructions parameter to _capture_messages() calls in
  _trace_get_response() and _trace_get_streaming_response()
- Extract instructions from chat_options in kwargs
- Add unit tests to verify system_instructions are captured correctly

When using ChatClient with ChatOptions.instructions, the OpenTelemetry
LLM span was missing system messages in gen_ai.input.messages and the
gen_ai.system_instructions attribute was not being set.

This fix aligns the ChatClient-level tracing with the Agent-level
tracing which already correctly passes system_instructions.

Fixes #3163

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Add edge case tests for system_instructions

- Add test for empty string instructions (should not set attribute)
- Add test for list-type instructions (verify multiple items captured)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Simplify: use options.get('instructions') directly instead of kwargs.get('chat_options')

Addresses reviewer feedback:
- Removed unnecessary chat_options variable from kwargs
- Directly access instructions from the options parameter
- Updated tests to use dict syntax for options (TypedDict convention)

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>

* Improve PR number handling in workflow (#3302)

* Improve PR number handling in workflow

Refine PR number extraction and validation method.

* Update .github/workflows/python-test-coverage-report.yml

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix error message for invalid PR number

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* .NET: Update Microsoft.Extensions.AI.* packages to 10.2.0 (#3211)

* Initial plan

* Update Microsoft.Extensions.AI.* to 10.2.0 and fix timestamp behavior tests

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

* .NET: Pass AdditionalProperties from parent to child when exposing an agent as a FunctionTool (#3219)

* Pass AdditionalProperties from parent to child when exposing an agent as a FunctionTool

* Rename variable to improve readability.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Python: [Breaking] Simplified Content types to a single class with classmethod constructors. (#3252)

* ported Content to a new model

* fixed linting

* fixes

* fixed data format handling

* fix for 3.10 mypy

* fix

* fix int test

* .NET: Durable Agent samples and automated validation for non-Azure Functions (#3042)

* Durable Agent samples and automated validation for non-Azure Functions

* Update test projects

* fix file encoding

* Remove AgentThreadMetadata usage

* Absorb breaking change from #3152

* Absorb newer breaking changes (AgentRunResponse --> AgentResponse)

* Absorb more breaking changes (see #3222)

* Improve integration test reliability (isolated task hubs, etc.)

* Fix flakey streaming test

---------

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>

* Python: fix(core): handle anyio cancel scope errors during MCP connection cleanup (#3277)

* fix(core): handle anyio cancel scope errors during MCP connection cleanup

* Address Copilot feedback

* Python: fix(ag-ui): properly handle json serialize with handoff workflows as agent (#3275)

* fix(ag-ui): properly handle json serialize with handoff workflows as agent

* Other improvements around handling non-serializable objects

* Bump tomli from 2.3.0 to 2.4.0 in /python (#3182)

Bumps [tomli](https://github.com/hukkin/tomli) from 2.3.0 to 2.4.0.
- [Changelog](https://github.com/hukkin/tomli/blob/master/CHANGELOG.md)
- [Commits](https://github.com/hukkin/tomli/compare/2.3.0...2.4.0)

---
updated-dependencies:
- dependency-name: tomli
  dependency-version: 2.4.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* .Net: Fix DebuggerDisplay attribute to reference existing property (#3326)

* Initial plan

* Fix DebuggerDisplay attribute to use Name instead of DisplayName

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* .NET: Add sample to show multiple AIContextProvider usage (#3284)

* Add sample to show multiple AIContextProvider usage

* Update comment.

* Update messaging in README.

* Address PR comments.

---------

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>

* .NET: Update Conversation Sample to use Conversation Id instead (#3180)

* Update Conversation Sample to use conversation Id instead

* Remove Run infix

* Remove the sync GetAIAgent from sample

* Python: Fix local MCP tools with `AzureAIProjectAgentProvider` (#3315)

* azureai v2 local mcp fix

* addressed copilot comments

* .NET: Improve readme for agents V2 (#3285)

* Improve readme for agents V2

* Architectural justification

* Update dotnet/samples/GettingStarted/FoundryAgents/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Bump pyasn1 from 0.6.1 to 0.6.2 in /python (#3257)

Bumps [pyasn1](https://github.com/pyasn1/pyasn1) from 0.6.1 to 0.6.2.
- [Release notes](https://github.com/pyasn1/pyasn1/releases)
- [Changelog](https://github.com/pyasn1/pyasn1/blob/main/CHANGES.rst)
- [Commits](https://github.com/pyasn1/pyasn1/compare/v0.6.1...v0.6.2)

---
updated-dependencies:
- dependency-name: pyasn1
  dependency-version: 0.6.2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* .NET: Fix DebuggerDisplay attribute in AIAgent.cs to reference existing properties (#2985)

* Initial plan

* Fix DebuggerDisplay attribute in AIAgent.cs to reference existing properties

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>

* Python: feat(anthropic): Add response_format support for structured outputs (#3301)

* fix(anthropic): Add response_format support for structured outputs

* only use from options

* use native way of response format

* ruff lint fix

* address comment; handle dict

* Updated package versions (#3335)

* Set min version of dependent azure-ai-projects to 2.0.0b3 (#3347)

* Adding feature collections ADR (#3332)

* .NET: [Breaking] Allow passing auth token credential to cosmosdb extensions (#3250)

* allow passing token credentials to cosmosdb extensions

* Update dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBWorkflowExtensions.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBWorkflowExtensions.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBChatExtensions.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: Subworkflows do not work well with HostAsAgent (#3240)

Subworkflows run into issues with Checkpointing and the Chat Protocol:

* The concurrency rework made subtle changes in behaviour that introduced a hang when using subworkflows with ChatProtocol and streaming execution.
* The ResetAsync() implementation in WorkflowHostExecutor was improperly resetting the joinContext - this was happening on restore checkpoint _after_ the join context was attached when
* Subworkflows cannot be used as the start node when hosted AsAgent due to inability to treat Catch-All as a Chat Protocol
* Subworkflow ownership issue when used in non-concurrent mode after finishing a run

Also fixes:
* When ChatMessages are output by executors that are not agents, there is no corresponding AgentResponseUpdate/AgentResponse event

Breaking Changes
* [BREAKING CHANGE] It is possible to provide the wrong RunId when resuming from CheckpointInfo (even though the data already exists on CheckpointInfo)

* Python: .NET: Executor source gen for workflow executor routing (#3131)

* Roslyn Source Generators for Workflow Executor Routing.

* Update dotnet/src/Microsoft.Agents.AI.Workflows.Generators/ExecutorRouteGenerator.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* WIP.

* All fixed up except dangling sends/yields attriutes, working on that next.

* Add protocol-only generation for SendsMessage/YieldsOutput attributes

* Ensuring collections that can change order are sorted to enable pipeline caching.

* Improvents per PR feedback.

---------

Co-authored-by: alliscode <bentho@microsoft.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* .NET: Joslat fix sample issue (#3270)

* adds support for labels in edges,  fixes rendering of labels in dot and mermaid, adds rendering of labels in edges

* Update dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* escaping edge labels, adding tests for labels containing strange characters that would break the diagram and enabling the previous signature so the API has backwards compatibility.

* Unify label in EdgeData

* Edge API adjustments, removed useless "sanitizer"

* fixed test

* Fix in Sample

* update

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Jacob Alber <jaalber@microsoft.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* .NET: Improve unit test coverage for Microsoft.Agents.AI.OpenAI (#3349)

* Initial plan

* Add unit tests for Microsoft.Agents.AI.OpenAI to improve code coverage

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Address code review feedback: remove unused using directives

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Fix format issues: file encoding and remove unused using directives

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Fix redundant cast error by using named parameter

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Remove excessive inline comments per PR review feedback

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Revert to main

* Python: Fix: Verify types during checkpoint deserialization to prevent marker spoofing (#3243)

* Initial plan

* Add validation for reserved keywords in checkpoint encoding/decoding

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Refactor to eliminate duplicate code in model protocol detection

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Fix pyright type narrowing issue for dataclass check

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Add comprehensive unit tests for checkpoint encoding

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Remove serialization-time reserved keyword validation to fix failing tests

The serialization-time validation was too aggressive and blocked legitimate use cases
where encoded data was being re-encoded. Security is now enforced only at deserialization
time by validating that classes marked with DATACLASS_MARKER are actual dataclasses and
classes marked with MODEL_MARKER actually support the model protocol.

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Apply ruff formatting to checkpoint encoding file

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Changes before error encountered

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Revert "Changes before error encountered"

This reverts commit f515b880dc.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: Tao Chen <taochen@microsoft.com>

* Python: Fix azurefunctions MCP tool invocation to use correct agent  (#3339)

* MCP tool fix for azurefunctions

* Moving logic to check for thread id

* Adding ReflectExecutors method to Workflow. (#3389)

* Fix merge conflicts

* Python: [BREAKING] simplify ag-ui run logic, fix mcp bugs, fix anthropic client issues in ag-ui (#3322)

* Refactor ag-ui to simplify flow

* Refactoring

* Fix backend tool

* Update tests

* Improvements

* Fix mypy

* Fixes

* Fix json serialize errors

* Python: fix(core): filter out internal args when passing kwargs to MCP tools (#3292)

* fix(core): filter conversation_id when passing kwargs to MCP tools

* Filter out options too

* Fix uv.lock conflict

* Python: Added tests for OpenAI content types + Unit test improvement (#3259)

* added tests for content types+ unit test improvement

* small fixes

* small fix

* Python: Prefer runtime `kwargs` for `conversation_id` in OpenAI Responses client (#3312)

* prefer kwargs conversation_id over options

* addressed comments

* Python: Azure AI mapping HostedImageGenerationTool to ImageGenTool (#3263)

* azureai image gen sample fix

* mypy fixes

* addressed comments + mapping updates

* image model fix

* content type fix

* Python: add(azure-ai): support reasoning config for AzureAIClient (#3403)

* add(azure-ai): support reasoning config for AzureAIClient

* Update sample

* Merge main

* improvements

* improve sample

* .NET: Allow overriding the ChatMessageStore to be used per agent run. (#3330)

* Allow overriding the ChatMessageStore to be used per agent run.

* Fix typos

* Fix Add and add TryAdd, Contains and Remove

* Update instructions to require automatically building and formatting (#3412)

* .NET: Rename ChatMessageStore to ChatHistoryProvider (#3375)

* Rename ChatMessageStore to ChatHistoryProvider

* Fix merge issue

* Fixed PR comments

* Fix tests after property rename

* Add unit tests and fix merge issues

* Fix encoding

---------

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Dina Suehiro Jones <dina.s.jones@intel.com>
Co-authored-by: Tao Chen <taochen@microsoft.com>
Co-authored-by: Kurt <65111699+q33566@users.noreply.github.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Co-authored-by: Korolev Dmitry <deagle.gross@gmail.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Co-authored-by: Jose Luis Latorre Millas <joslat@gmail.com>
Co-authored-by: Jacob Alber <jaalber@microsoft.com>
Co-authored-by: Richard Ortega <richardjortega@gmail.com>
Co-authored-by: 刘邦学AI <lbbniu@gmail.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Nico Möller <nkm-moeller@mail.de>
Co-authored-by: Chris Gillum <cgillum@microsoft.com>
Co-authored-by: Giles Odigwe <79032838+giles17@users.noreply.github.com>
Co-authored-by: Phillip Hoff <phillip.hoff@gmail.com>
Co-authored-by: Ege Ozan Özyedek <36128615+egeozanozyedek@users.noreply.github.com>
Co-authored-by: samueljohnsiby <66901393+samueljohnsiby@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
Co-authored-by: Hao Luo <338265+howlowck@users.noreply.github.com>
Co-authored-by: Victor Dibia <chuvidi2003@gmail.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: Jacob Viau <javia@microsoft.com>
Co-authored-by: SuperKenVery <39673849+SuperKenVery@users.noreply.github.com>
Co-authored-by: Sunil Dutta <dutta.2003@gmail.com>
Co-authored-by: Sunil Dutta <sunil.dutta@penske.com>
Co-authored-by: budgetboardingai <apurva.sharma31@gmail.com>
Co-authored-by: Syrine Chelly <62653967+SyChell@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <sergemenshikh@gmail.com>
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
Co-authored-by: takanori-terai <123897708+takanori-terai@users.noreply.github.com>
Co-authored-by: claude89757 <138977524+claude89757@users.noreply.github.com>
Co-authored-by: Gavin Aguiar <80794152+gavin-aguiar@users.noreply.github.com>
Co-authored-by: Sukeesh <vsukeeshbabu@gmail.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>
Co-authored-by: Ao Chen <chenao3220@gmail.com>
Co-authored-by: Dina Suehiro Jones <dina.s.jones@intel.com>
Co-authored-by: eoindoherty1 <eoindoherty@microsoft.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Darren Cohen <39422044+dargilco@users.noreply.github.com>
Co-authored-by: Ben Thomas <ben.thomas@microsoft.com>
Co-authored-by: alliscode <bentho@microsoft.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: Shyju Krishnankutty <connectshyju@gmail.com>
This commit is contained in:
Laveesh Rohra
2026-01-23 10:31:54 -08:00
committed by GitHub
co-authored by TaoChenOSU copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Tao Chen Kurt Evan Mattson SergeyMenshykh Korolev Dmitry Mark Wallace Copilot rogerbarreto Copilot dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Eduard van Valkenburg Dmytro Struk Chris Jose Luis Latorre Millas Jacob Alber Richard Ortega 刘邦学AI Stephen Toub Nico Möller Chris Gillum Giles Odigwe Phillip Hoff Ege Ozan Özyedek samueljohnsiby Evan Mattson Hao Luo Victor Dibia stephentoub Jacob Viau SuperKenVery Sunil Dutta Sunil Dutta budgetboardingai Syrine Chelly SergeyMenshykh westey takanori-terai claude89757 Gavin Aguiar Sukeesh eavanvalkenburg eavanvalkenburg Ao Chen Dina Suehiro Jones eoindoherty1 Claude Opus 4.5 Darren Cohen Ben Thomas alliscode Shyju Krishnankutty
parent ff839435a2
commit 172423aab7
552 changed files with 23634 additions and 14420 deletions
@@ -18,7 +18,6 @@ from agent_framework import (
ChatResponse,
ChatResponseUpdate,
HostedCodeInterpreterTool,
TextContent,
)
from agent_framework.azure import AzureOpenAIAssistantsClient
from agent_framework.exceptions import ServiceInitializationError
@@ -332,7 +331,7 @@ async def test_azure_assistants_client_streaming() -> None:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
assert any(word in full_message.lower() for word in ["sunny", "25", "weather", "seattle"])
@@ -358,7 +357,7 @@ async def test_azure_assistants_client_streaming_tools() -> None:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
assert any(word in full_message.lower() for word in ["sunny", "25", "weather"])
@@ -25,7 +25,6 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
TextContent,
ai_function,
)
from agent_framework._telemetry import USER_AGENT_KEY
@@ -304,9 +303,9 @@ async def test_azure_on_your_data(
)
assert len(content.messages) == 1
assert len(content.messages[0].contents) == 1
assert isinstance(content.messages[0].contents[0], TextContent)
assert content.messages[0].contents[0].type == "text"
assert len(content.messages[0].contents[0].annotations) == 1
assert content.messages[0].contents[0].annotations[0].title == "test title"
assert content.messages[0].contents[0].annotations[0]["title"] == "test title"
assert content.messages[0].contents[0].text == "test"
mock_create.assert_awaited_once_with(
@@ -374,9 +373,9 @@ async def test_azure_on_your_data_string(
)
assert len(content.messages) == 1
assert len(content.messages[0].contents) == 1
assert isinstance(content.messages[0].contents[0], TextContent)
assert content.messages[0].contents[0].type == "text"
assert len(content.messages[0].contents[0].annotations) == 1
assert content.messages[0].contents[0].annotations[0].title == "test title"
assert content.messages[0].contents[0].annotations[0]["title"] == "test title"
assert content.messages[0].contents[0].text == "test"
mock_create.assert_awaited_once_with(
@@ -433,7 +432,7 @@ async def test_azure_on_your_data_fail(
)
assert len(content.messages) == 1
assert len(content.messages[0].contents) == 1
assert isinstance(content.messages[0].contents[0], TextContent)
assert content.messages[0].contents[0].type == "text"
assert content.messages[0].contents[0].text == "test"
mock_create.assert_awaited_once_with(
@@ -592,6 +591,46 @@ async def test_get_streaming(
)
@patch.object(AsyncChatCompletions, "create", new_callable=AsyncMock)
async def test_streaming_with_none_delta(
mock_create: AsyncMock,
azure_openai_unit_test_env: dict[str, str],
chat_history: list[ChatMessage],
) -> None:
"""Test streaming handles None delta from async content filtering."""
# First chunk has None delta (simulates async filtering)
chunk_choice_with_none = ChunkChoice.model_construct(index=0, delta=None, finish_reason=None)
chunk_with_none_delta = ChatCompletionChunk.model_construct(
id="test_id",
choices=[chunk_choice_with_none],
created=0,
model="test",
object="chat.completion.chunk",
)
# Second chunk has actual content
chunk_with_content = ChatCompletionChunk(
id="test_id",
choices=[ChunkChoice(index=0, delta=ChunkChoiceDelta(content="test", role="assistant"), finish_reason="stop")],
created=0,
model="test",
object="chat.completion.chunk",
)
stream = MagicMock(spec=AsyncStream)
stream.__aiter__.return_value = [chunk_with_none_delta, chunk_with_content]
mock_create.return_value = stream
chat_history.append(ChatMessage(text="hello world", role="user"))
azure_chat_client = AzureOpenAIChatClient()
results: list[ChatResponseUpdate] = []
async for msg in azure_chat_client.get_streaming_response(messages=chat_history):
results.append(msg)
assert len(results) > 0
assert any(content.type == "text" and content.text == "test" for msg in results for content in msg.contents)
assert any(msg.contents for msg in results)
@ai_function
def get_story_text() -> str:
"""Returns a story about Emily and David."""
@@ -689,7 +728,7 @@ async def test_azure_openai_chat_client_streaming() -> None:
assert chunk.message_id is not None
assert chunk.response_id is not None
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
assert "Emily" in full_message or "David" in full_message
@@ -715,7 +754,7 @@ async def test_azure_openai_chat_client_streaming_tools() -> None:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
assert "Emily" in full_message or "David" in full_message
@@ -15,10 +15,10 @@ from agent_framework import (
ChatClientProtocol,
ChatMessage,
ChatResponse,
Content,
HostedCodeInterpreterTool,
HostedFileSearchTool,
HostedMCPTool,
HostedVectorStoreContent,
HostedWebSearchTool,
ai_function,
)
@@ -48,7 +48,7 @@ async def get_weather(location: Annotated[str, "The location as a city name"]) -
return f"The weather in {location} is sunny and 72°F."
async def create_vector_store(client: AzureOpenAIResponsesClient) -> tuple[str, HostedVectorStoreContent]:
async def create_vector_store(client: AzureOpenAIResponsesClient) -> tuple[str, Content]:
"""Create a vector store with sample documents for testing."""
file = await client.client.files.create(
file=("todays_weather.txt", b"The weather today is sunny with a high of 75F."), purpose="assistants"
@@ -61,7 +61,7 @@ async def create_vector_store(client: AzureOpenAIResponsesClient) -> tuple[str,
if result.last_error is not None:
raise Exception(f"Vector store file processing failed with status: {result.last_error.message}")
return file.id, HostedVectorStoreContent(vector_store_id=vector_store.id)
return file.id, Content.from_hosted_vector_store(vector_store_id=vector_store.id)
async def delete_vector_store(client: AzureOpenAIResponsesClient, file_id: str, vector_store_id: str) -> None:
+5 -5
View File
@@ -20,8 +20,8 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
Content,
Role,
TextContent,
ToolProtocol,
ai_function,
use_chat_middleware,
@@ -108,8 +108,8 @@ class MockChatClient:
for update in self.streaming_responses.pop(0):
yield update
else:
yield ChatResponseUpdate(text=TextContent(text="test streaming response "), role="assistant")
yield ChatResponseUpdate(contents=[TextContent(text="another update")], role="assistant")
yield ChatResponseUpdate(text=Content.from_text(text="test streaming response "), role="assistant")
yield ChatResponseUpdate(contents=[Content.from_text(text="another update")], role="assistant")
@use_chat_middleware
@@ -233,7 +233,7 @@ class MockAgent(AgentProtocol):
**kwargs: Any,
) -> AgentResponse:
logger.debug(f"Running mock agent, with: {messages=}, {thread=}, {kwargs=}")
return AgentResponse(messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent("Response")])])
return AgentResponse(messages=[ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text("Response")])])
async def run_stream(
self,
@@ -243,7 +243,7 @@ class MockAgent(AgentProtocol):
**kwargs: Any,
) -> AsyncIterable[AgentResponseUpdate]:
logger.debug(f"Running mock agent stream, with: {messages=}, {thread=}, {kwargs=}")
yield AgentResponseUpdate(contents=[TextContent("Response")])
yield AgentResponseUpdate(contents=[Content.from_text("Response")])
def get_new_thread(self) -> AgentThread:
return MockAgentThread()
+10 -7
View File
@@ -18,12 +18,11 @@ from agent_framework import (
ChatMessage,
ChatMessageStore,
ChatResponse,
Content,
Context,
ContextProvider,
FunctionCallContent,
HostedCodeInterpreterTool,
Role,
TextContent,
ai_function,
)
from agent_framework._mcp import MCPTool
@@ -136,7 +135,7 @@ async def test_prepare_thread_does_not_mutate_agent_chat_options(chat_client: Ch
async def test_chat_client_agent_update_thread_id(chat_client_base: ChatClientProtocol) -> None:
mock_response = ChatResponse(
messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent("test response")])],
messages=[ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text("test response")])],
conversation_id="123",
)
chat_client_base.run_responses = [mock_response]
@@ -200,7 +199,9 @@ async def test_chat_client_agent_author_name_is_used_from_response(chat_client_b
chat_client_base.run_responses = [
ChatResponse(
messages=[
ChatMessage(role=Role.ASSISTANT, contents=[TextContent("test response")], author_name="TestAuthor")
ChatMessage(
role=Role.ASSISTANT, contents=[Content.from_text("test response")], author_name="TestAuthor"
)
]
)
]
@@ -264,7 +265,7 @@ async def test_chat_agent_context_providers_thread_created(chat_client_base: Cha
mock_provider = MockContextProvider()
chat_client_base.run_responses = [
ChatResponse(
messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent("test response")])],
messages=[ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text("test response")])],
conversation_id="test-thread-id",
)
]
@@ -345,7 +346,7 @@ async def test_chat_agent_context_providers_with_thread_service_id(chat_client_b
mock_provider = MockContextProvider()
chat_client_base.run_responses = [
ChatResponse(
messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent("test response")])],
messages=[ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text("test response")])],
conversation_id="service-thread-123",
)
]
@@ -575,7 +576,9 @@ async def test_agent_tool_receives_thread_in_kwargs(chat_client_base: Any) -> No
ChatResponse(
messages=ChatMessage(
role="assistant",
contents=[FunctionCallContent(call_id="1", name="echo_thread_info", arguments='{"text": "hello"}')],
contents=[
Content.from_function_call(call_id="1", name="echo_thread_info", arguments='{"text": "hello"}')
],
)
),
ChatResponse(messages=ChatMessage(role="assistant", text="done")),
@@ -5,7 +5,7 @@
from collections.abc import Awaitable, Callable
from typing import Any
from agent_framework import ChatAgent, ChatMessage, ChatResponse, FunctionCallContent, agent_middleware
from agent_framework import ChatAgent, ChatMessage, ChatResponse, Content, agent_middleware
from agent_framework._middleware import AgentRunContext
from .conftest import MockChatClient
@@ -113,7 +113,7 @@ class TestAsToolKwargsPropagation:
ChatMessage(
role="assistant",
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_c_1",
name="call_c",
arguments='{"task": "Please execute agent_c"}',
@@ -170,10 +170,10 @@ class TestAsToolKwargsPropagation:
await next(context)
# Setup mock streaming responses
from agent_framework import ChatResponseUpdate, TextContent
from agent_framework import ChatResponseUpdate
chat_client.streaming_responses = [
[ChatResponseUpdate(text=TextContent(text="Streaming response"), role="assistant")],
[ChatResponseUpdate(text=Content.from_text(text="Streaming response"), role="assistant")],
]
sub_agent = ChatAgent(
@@ -313,3 +313,44 @@ class TestAsToolKwargsPropagation:
# Verify second call had its own kwargs (not leaked from first)
assert second_call_kwargs.get("session_id") == "session-2"
assert second_call_kwargs.get("api_token") == "token-2"
async def test_as_tool_excludes_conversation_id_from_forwarded_kwargs(self, chat_client: MockChatClient) -> None:
"""Test that conversation_id is not forwarded to sub-agent."""
captured_kwargs: dict[str, Any] = {}
@agent_middleware
async def capture_middleware(
context: AgentRunContext, next: Callable[[AgentRunContext], Awaitable[None]]
) -> None:
captured_kwargs.update(context.kwargs)
await next(context)
# Setup mock response
chat_client.responses = [
ChatResponse(messages=[ChatMessage(role="assistant", text="Response from sub-agent")]),
]
sub_agent = ChatAgent(
chat_client=chat_client,
name="sub_agent",
middleware=[capture_middleware],
)
tool = sub_agent.as_tool(name="delegate", arg_name="task")
# Invoke tool with conversation_id in kwargs (simulating parent's conversation state)
await tool.invoke(
arguments=tool.input_model(task="Test delegation"),
conversation_id="conv-parent-456",
api_token="secret-xyz-123",
user_id="user-456",
)
# Verify conversation_id was NOT forwarded to sub-agent
assert "conversation_id" not in captured_kwargs, (
f"conversation_id should not be forwarded, but got: {captured_kwargs}"
)
# Verify other kwargs were still forwarded
assert captured_kwargs.get("api_token") == "secret-xyz-123"
assert captured_kwargs.get("user_id") == "user-456"
File diff suppressed because it is too large Load Diff
@@ -8,8 +8,7 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
FunctionCallContent,
TextContent,
Content,
ai_function,
)
from agent_framework._tools import _handle_function_calls_response, _handle_function_calls_streaming_response
@@ -42,7 +41,9 @@ class TestKwargsPropagationToAIFunction:
ChatMessage(
role="assistant",
contents=[
FunctionCallContent(call_id="call_1", name="capture_kwargs_tool", arguments='{"x": 42}')
Content.from_function_call(
call_id="call_1", name="capture_kwargs_tool", arguments='{"x": 42}'
)
],
)
]
@@ -94,7 +95,9 @@ class TestKwargsPropagationToAIFunction:
messages=[
ChatMessage(
role="assistant",
contents=[FunctionCallContent(call_id="call_1", name="simple_tool", arguments='{"x": 99}')],
contents=[
Content.from_function_call(call_id="call_1", name="simple_tool", arguments='{"x": 99}')
],
)
]
)
@@ -136,10 +139,10 @@ class TestKwargsPropagationToAIFunction:
ChatMessage(
role="assistant",
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_1", name="tracking_tool", arguments='{"name": "first"}'
),
FunctionCallContent(
Content.from_function_call(
call_id="call_2", name="tracking_tool", arguments='{"name": "second"}'
),
],
@@ -187,7 +190,7 @@ class TestKwargsPropagationToAIFunction:
yield ChatResponseUpdate(
role="assistant",
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="stream_call_1",
name="streaming_capture_tool",
arguments='{"value": "streaming-test"}',
@@ -197,7 +200,9 @@ class TestKwargsPropagationToAIFunction:
)
else:
# Second call: return final response
yield ChatResponseUpdate(text=TextContent(text="Stream complete!"), role="assistant", is_finished=True)
yield ChatResponseUpdate(
text=Content.from_text(text="Stream complete!"), role="assistant", is_finished=True
)
wrapped = _handle_function_calls_streaming_response(mock_get_streaming_response)
+191 -41
View File
@@ -13,14 +13,12 @@ from pydantic import AnyUrl, BaseModel, ValidationError
from agent_framework import (
ChatMessage,
DataContent,
Content,
MCPStdioTool,
MCPStreamableHTTPTool,
MCPWebsocketTool,
Role,
TextContent,
ToolProtocol,
UriContent,
)
from agent_framework._mcp import (
MCPTool,
@@ -65,7 +63,7 @@ def test_mcp_prompt_message_to_ai_content():
assert isinstance(ai_content, ChatMessage)
assert ai_content.role.value == "user"
assert len(ai_content.contents) == 1
assert isinstance(ai_content.contents[0], TextContent)
assert ai_content.contents[0].type == "text"
assert ai_content.contents[0].text == "Hello, world!"
assert ai_content.raw_representation == mcp_message
@@ -75,20 +73,20 @@ def test_parse_contents_from_mcp_tool_result():
mcp_result = types.CallToolResult(
content=[
types.TextContent(type="text", text="Result text"),
types.ImageContent(type="image", data="xyz", mimeType="image/png"),
types.ImageContent(type="image", data=b"abc", mimeType="image/webp"),
types.ImageContent(type="image", data="eHl6", mimeType="image/png"), # base64 for "xyz"
types.ImageContent(type="image", data="YWJj", mimeType="image/webp"), # base64 for "abc"
]
)
ai_contents = _parse_contents_from_mcp_tool_result(mcp_result)
assert len(ai_contents) == 3
assert isinstance(ai_contents[0], TextContent)
assert ai_contents[0].type == "text"
assert ai_contents[0].text == "Result text"
assert isinstance(ai_contents[1], DataContent)
assert ai_contents[1].uri == "data:image/png;base64,xyz"
assert ai_contents[1].type == "data"
assert ai_contents[1].uri == "data:image/png;base64,eHl6"
assert ai_contents[1].media_type == "image/png"
assert isinstance(ai_contents[2], DataContent)
assert ai_contents[2].uri == "data:image/webp;base64,abc"
assert ai_contents[2].type == "data"
assert ai_contents[2].uri == "data:image/webp;base64,YWJj"
assert ai_contents[2].media_type == "image/webp"
@@ -103,7 +101,7 @@ def test_mcp_call_tool_result_with_meta_error():
ai_contents = _parse_contents_from_mcp_tool_result(mcp_result)
assert len(ai_contents) == 1
assert isinstance(ai_contents[0], TextContent)
assert ai_contents[0].type == "text"
assert ai_contents[0].text == "Error occurred"
# Check that _meta data is merged into additional_properties
@@ -134,7 +132,7 @@ def test_mcp_call_tool_result_with_meta_arbitrary_data():
ai_contents = _parse_contents_from_mcp_tool_result(mcp_result)
assert len(ai_contents) == 1
assert isinstance(ai_contents[0], TextContent)
assert ai_contents[0].type == "text"
assert ai_contents[0].text == "Success result"
# Check that _meta data is preserved in additional_properties
@@ -172,7 +170,7 @@ def test_mcp_call_tool_result_with_meta_none():
ai_contents = _parse_contents_from_mcp_tool_result(mcp_result)
assert len(ai_contents) == 1
assert isinstance(ai_contents[0], TextContent)
assert ai_contents[0].type == "text"
assert ai_contents[0].text == "No meta test"
# Should handle gracefully when no _meta field exists
@@ -187,7 +185,7 @@ def test_mcp_call_tool_result_regression_successful_workflow():
mcp_result = types.CallToolResult(
content=[
types.TextContent(type="text", text="Success message"),
types.ImageContent(type="image", data="abc123", mimeType="image/jpeg"),
types.ImageContent(type="image", data="YWJjMTIz", mimeType="image/jpeg"), # base64 for "abc123"
]
)
@@ -197,12 +195,12 @@ def test_mcp_call_tool_result_regression_successful_workflow():
assert len(ai_contents) == 2
text_content = ai_contents[0]
assert isinstance(text_content, TextContent)
assert text_content.type == "text"
assert text_content.text == "Success message"
image_content = ai_contents[1]
assert isinstance(image_content, DataContent)
assert image_content.uri == "data:image/jpeg;base64,abc123"
assert image_content.type == "data"
assert image_content.uri == "data:image/jpeg;base64,YWJjMTIz"
assert image_content.media_type == "image/jpeg"
# Should have no additional_properties when no _meta field
@@ -215,30 +213,31 @@ def test_mcp_content_types_to_ai_content_text():
mcp_content = types.TextContent(type="text", text="Sample text")
ai_content = _parse_content_from_mcp(mcp_content)[0]
assert isinstance(ai_content, TextContent)
assert ai_content.type == "text"
assert ai_content.text == "Sample text"
assert ai_content.raw_representation == mcp_content
def test_mcp_content_types_to_ai_content_image():
"""Test conversion of MCP image content to AI content."""
mcp_content = types.ImageContent(type="image", data="abc", mimeType="image/jpeg")
mcp_content = types.ImageContent(type="image", data=b"abc", mimeType="image/jpeg")
# MCP can send data as base64 string or as bytes
mcp_content = types.ImageContent(type="image", data="YWJj", mimeType="image/jpeg") # base64 for b"abc"
ai_content = _parse_content_from_mcp(mcp_content)[0]
assert isinstance(ai_content, DataContent)
assert ai_content.uri == "data:image/jpeg;base64,abc"
assert ai_content.type == "data"
assert ai_content.uri == "data:image/jpeg;base64,YWJj"
assert ai_content.media_type == "image/jpeg"
assert ai_content.raw_representation == mcp_content
def test_mcp_content_types_to_ai_content_audio():
"""Test conversion of MCP audio content to AI content."""
mcp_content = types.AudioContent(type="audio", data="def", mimeType="audio/wav")
# Use properly padded base64
mcp_content = types.AudioContent(type="audio", data="ZGVm", mimeType="audio/wav") # base64 for b"def"
ai_content = _parse_content_from_mcp(mcp_content)[0]
assert isinstance(ai_content, DataContent)
assert ai_content.uri == "data:audio/wav;base64,def"
assert ai_content.type == "data"
assert ai_content.uri == "data:audio/wav;base64,ZGVm"
assert ai_content.media_type == "audio/wav"
assert ai_content.raw_representation == mcp_content
@@ -253,7 +252,7 @@ def test_mcp_content_types_to_ai_content_resource_link():
)
ai_content = _parse_content_from_mcp(mcp_content)[0]
assert isinstance(ai_content, UriContent)
assert ai_content.type == "uri"
assert ai_content.uri == "https://example.com/resource"
assert ai_content.media_type == "application/json"
assert ai_content.raw_representation == mcp_content
@@ -269,7 +268,7 @@ def test_mcp_content_types_to_ai_content_embedded_resource_text():
mcp_content = types.EmbeddedResource(type="resource", resource=text_resource)
ai_content = _parse_content_from_mcp(mcp_content)[0]
assert isinstance(ai_content, TextContent)
assert ai_content.type == "text"
assert ai_content.text == "Embedded text content"
assert ai_content.raw_representation == mcp_content
@@ -285,7 +284,7 @@ def test_mcp_content_types_to_ai_content_embedded_resource_blob():
mcp_content = types.EmbeddedResource(type="resource", resource=blob_resource)
ai_content = _parse_content_from_mcp(mcp_content)[0]
assert isinstance(ai_content, DataContent)
assert ai_content.type == "data"
assert ai_content.uri == "data:application/octet-stream;base64,dGVzdCBkYXRh"
assert ai_content.media_type == "application/octet-stream"
assert ai_content.raw_representation == mcp_content
@@ -293,7 +292,7 @@ def test_mcp_content_types_to_ai_content_embedded_resource_blob():
def test_ai_content_to_mcp_content_types_text():
"""Test conversion of AI text content to MCP content."""
ai_content = TextContent(text="Sample text")
ai_content = Content.from_text(text="Sample text")
mcp_content = _prepare_content_for_mcp(ai_content)
assert isinstance(mcp_content, types.TextContent)
@@ -303,7 +302,7 @@ def test_ai_content_to_mcp_content_types_text():
def test_ai_content_to_mcp_content_types_data_image():
"""Test conversion of AI data content to MCP content."""
ai_content = DataContent(uri="data:image/png;base64,xyz", media_type="image/png")
ai_content = Content.from_uri(uri="data:image/png;base64,xyz", media_type="image/png")
mcp_content = _prepare_content_for_mcp(ai_content)
assert isinstance(mcp_content, types.ImageContent)
@@ -314,7 +313,7 @@ def test_ai_content_to_mcp_content_types_data_image():
def test_ai_content_to_mcp_content_types_data_audio():
"""Test conversion of AI data content to MCP content."""
ai_content = DataContent(uri="data:audio/mpeg;base64,xyz", media_type="audio/mpeg")
ai_content = Content.from_uri(uri="data:audio/mpeg;base64,xyz", media_type="audio/mpeg")
mcp_content = _prepare_content_for_mcp(ai_content)
assert isinstance(mcp_content, types.AudioContent)
@@ -325,7 +324,7 @@ def test_ai_content_to_mcp_content_types_data_audio():
def test_ai_content_to_mcp_content_types_data_binary():
"""Test conversion of AI data content to MCP content."""
ai_content = DataContent(
ai_content = Content.from_uri(
uri="data:application/octet-stream;base64,xyz",
media_type="application/octet-stream",
)
@@ -339,7 +338,7 @@ def test_ai_content_to_mcp_content_types_data_binary():
def test_ai_content_to_mcp_content_types_uri():
"""Test conversion of AI URI content to MCP content."""
ai_content = UriContent(uri="https://example.com/resource", media_type="application/json")
ai_content = Content.from_uri(uri="https://example.com/resource", media_type="application/json")
mcp_content = _prepare_content_for_mcp(ai_content)
assert isinstance(mcp_content, types.ResourceLink)
@@ -352,8 +351,8 @@ def test_prepare_message_for_mcp():
message = ChatMessage(
role="user",
contents=[
TextContent(text="test"),
DataContent(uri="data:image/png;base64,xyz", media_type="image/png"),
Content.from_text(text="test"),
Content.from_uri(uri="data:image/png;base64,xyz", media_type="image/png"),
],
)
mcp_contents = _prepare_message_for_mcp(message)
@@ -871,7 +870,7 @@ async def test_mcp_tool_call_tool_with_meta_integration():
result = await func.invoke(param="test_value")
assert len(result) == 1
assert isinstance(result[0], TextContent)
assert result[0].type == "text"
assert result[0].text == "Tool executed with metadata"
# Verify that _meta data is present in additional_properties
@@ -920,7 +919,7 @@ async def test_local_mcp_server_function_execution():
result = await func.invoke(param="test_value")
assert len(result) == 1
assert isinstance(result[0], TextContent)
assert result[0].type == "text"
assert result[0].text == "Tool executed successfully"
@@ -969,7 +968,7 @@ async def test_local_mcp_server_function_execution_with_nested_object():
result = await func.invoke(params={"customer_id": 251})
assert len(result) == 1
assert isinstance(result[0], TextContent)
assert result[0].type == "text"
# Verify the session.call_tool was called with the correct nested structure
server.session.call_tool.assert_called_once()
@@ -1413,7 +1412,7 @@ async def test_mcp_tool_sampling_callback_chat_client_exception():
async def test_mcp_tool_sampling_callback_no_valid_content():
"""Test sampling callback when response has no valid content types."""
from agent_framework import ChatMessage, DataContent, Role
from agent_framework import ChatMessage, Role
tool = MCPStdioTool(name="test_tool", command="python")
@@ -1424,7 +1423,7 @@ async def test_mcp_tool_sampling_callback_no_valid_content():
ChatMessage(
role=Role.ASSISTANT,
contents=[
DataContent(
Content.from_uri(
uri="data:application/json;base64,e30K",
media_type="application/json",
)
@@ -2364,3 +2363,154 @@ async def test_mcp_tool_get_prompt_reconnection_on_closed_resource_error():
assert mock_connect.call_count >= 1
# Verify error message indicates reconnection failure
assert "failed to reconnect" in str(exc_info.value).lower()
async def test_mcp_tool_reconnection_handles_cross_task_cancel_scope_error():
"""Test that reconnection gracefully handles anyio cancel scope errors.
This tests the fix for the bug where calling connect(reset=True) from a
different task than where the connection was originally established would
cause: RuntimeError: Attempted to exit cancel scope in a different task
than it was entered in
This happens when using multiple MCP tools with AG-UI streaming - the first
tool call succeeds, but when the connection closes, the second tool call
triggers a reconnection from within the streaming loop (a different task).
"""
from contextlib import AsyncExitStack
from agent_framework._mcp import MCPStdioTool
# Use load_tools=False and load_prompts=False to avoid triggering them during connect()
tool = MCPStdioTool(
name="test_server",
command="test_command",
args=["arg1"],
load_tools=False,
load_prompts=False,
)
# Mock the exit stack to raise the cross-task cancel scope error
mock_exit_stack = AsyncMock(spec=AsyncExitStack)
mock_exit_stack.aclose = AsyncMock(
side_effect=RuntimeError("Attempted to exit cancel scope in a different task than it was entered in")
)
tool._exit_stack = mock_exit_stack
tool.session = Mock()
tool.is_connected = True
# Mock get_mcp_client to return a mock transport
mock_transport = (Mock(), Mock())
mock_context = AsyncMock()
mock_context.__aenter__ = AsyncMock(return_value=mock_transport)
mock_context.__aexit__ = AsyncMock()
with (
patch.object(tool, "get_mcp_client", return_value=mock_context),
patch("agent_framework._mcp.ClientSession") as mock_session_class,
):
mock_session = Mock()
mock_session._request_id = 1
mock_session.initialize = AsyncMock()
mock_session.set_logging_level = AsyncMock()
mock_session_context = AsyncMock()
mock_session_context.__aenter__ = AsyncMock(return_value=mock_session)
mock_session_context.__aexit__ = AsyncMock()
mock_session_class.return_value = mock_session_context
# This should NOT raise even though aclose() raised the cancel scope error
# The _safe_close_exit_stack method should catch and log the error
await tool.connect(reset=True)
# Verify a new exit stack was created (the old mock was replaced)
assert tool._exit_stack is not mock_exit_stack
assert tool.session is not None
assert tool.is_connected is True
async def test_mcp_tool_safe_close_reraises_other_runtime_errors():
"""Test that _safe_close_exit_stack re-raises RuntimeErrors that aren't cancel scope related."""
from contextlib import AsyncExitStack
from agent_framework._mcp import MCPStdioTool
tool = MCPStdioTool(
name="test_server",
command="test_command",
args=["arg1"],
load_tools=True,
)
# Mock the exit stack to raise a different RuntimeError
mock_exit_stack = AsyncMock(spec=AsyncExitStack)
mock_exit_stack.aclose = AsyncMock(side_effect=RuntimeError("Some other runtime error"))
tool._exit_stack = mock_exit_stack
# This should re-raise the RuntimeError since it's not about cancel scopes
with pytest.raises(RuntimeError) as exc_info:
await tool._safe_close_exit_stack()
assert "Some other runtime error" in str(exc_info.value)
async def test_mcp_tool_safe_close_handles_alternate_cancel_scope_error():
"""Test that _safe_close_exit_stack handles the alternate cancel scope error message.
anyio has multiple variants of cancel scope errors:
- "Attempted to exit cancel scope in a different task than it was entered in"
- "Attempted to exit a cancel scope that isn't the current task's current cancel scope"
"""
from contextlib import AsyncExitStack
from agent_framework._mcp import MCPStdioTool
tool = MCPStdioTool(
name="test_server",
command="test_command",
args=["arg1"],
load_tools=False,
load_prompts=False,
)
# Mock the exit stack to raise the alternate cancel scope error
mock_exit_stack = AsyncMock(spec=AsyncExitStack)
mock_exit_stack.aclose = AsyncMock(
side_effect=RuntimeError("Attempted to exit a cancel scope that isn't the current task's current cancel scope")
)
tool._exit_stack = mock_exit_stack
# This should NOT raise - the error should be caught and logged
await tool._safe_close_exit_stack()
# Verify aclose was called
mock_exit_stack.aclose.assert_called_once()
async def test_mcp_tool_safe_close_handles_cancelled_error():
"""Test that _safe_close_exit_stack handles asyncio.CancelledError.
CancelledError can occur during cleanup when anyio cancel scopes are involved.
"""
import asyncio
from contextlib import AsyncExitStack
from agent_framework._mcp import MCPStdioTool
tool = MCPStdioTool(
name="test_server",
command="test_command",
args=["arg1"],
load_tools=False,
load_prompts=False,
)
# Mock the exit stack to raise CancelledError
mock_exit_stack = AsyncMock(spec=AsyncExitStack)
mock_exit_stack.aclose = AsyncMock(side_effect=asyncio.CancelledError())
tool._exit_stack = mock_exit_stack
# This should NOT raise - the CancelledError should be caught and logged
await tool._safe_close_exit_stack()
# Verify aclose was called
mock_exit_stack.aclose.assert_called_once()
@@ -14,8 +14,8 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
Content,
Role,
TextContent,
)
from agent_framework._middleware import (
AgentMiddleware,
@@ -217,8 +217,8 @@ class TestAgentMiddlewarePipeline:
context = AgentRunContext(agent=mock_agent, messages=messages)
async def final_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(contents=[TextContent(text="chunk1")])
yield AgentResponseUpdate(contents=[TextContent(text="chunk2")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk2")])
updates: list[AgentResponseUpdate] = []
async for update in pipeline.execute_stream(mock_agent, messages, context, final_handler):
@@ -250,8 +250,8 @@ class TestAgentMiddlewarePipeline:
async def final_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
execution_order.append("handler_start")
yield AgentResponseUpdate(contents=[TextContent(text="chunk1")])
yield AgentResponseUpdate(contents=[TextContent(text="chunk2")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk2")])
execution_order.append("handler_end")
updates: list[AgentResponseUpdate] = []
@@ -313,8 +313,8 @@ class TestAgentMiddlewarePipeline:
async def final_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
# Handler should not be executed when terminated before next()
execution_order.append("handler_start")
yield AgentResponseUpdate(contents=[TextContent(text="chunk1")])
yield AgentResponseUpdate(contents=[TextContent(text="chunk2")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk2")])
execution_order.append("handler_end")
updates: list[AgentResponseUpdate] = []
@@ -336,8 +336,8 @@ class TestAgentMiddlewarePipeline:
async def final_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
execution_order.append("handler_start")
yield AgentResponseUpdate(contents=[TextContent(text="chunk1")])
yield AgentResponseUpdate(contents=[TextContent(text="chunk2")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk2")])
execution_order.append("handler_end")
updates: list[AgentResponseUpdate] = []
@@ -609,8 +609,8 @@ class TestChatMiddlewarePipeline:
context = ChatContext(chat_client=mock_chat_client, messages=messages, options=chat_options)
async def final_handler(ctx: ChatContext) -> AsyncIterable[ChatResponseUpdate]:
yield ChatResponseUpdate(contents=[TextContent(text="chunk1")])
yield ChatResponseUpdate(contents=[TextContent(text="chunk2")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk2")])
updates: list[ChatResponseUpdate] = []
async for update in pipeline.execute_stream(mock_chat_client, messages, chat_options, context, final_handler):
@@ -641,8 +641,8 @@ class TestChatMiddlewarePipeline:
async def final_handler(ctx: ChatContext) -> AsyncIterable[ChatResponseUpdate]:
execution_order.append("handler_start")
yield ChatResponseUpdate(contents=[TextContent(text="chunk1")])
yield ChatResponseUpdate(contents=[TextContent(text="chunk2")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk2")])
execution_order.append("handler_end")
updates: list[ChatResponseUpdate] = []
@@ -706,8 +706,8 @@ class TestChatMiddlewarePipeline:
async def final_handler(ctx: ChatContext) -> AsyncIterable[ChatResponseUpdate]:
# Handler should not be executed when terminated before next()
execution_order.append("handler_start")
yield ChatResponseUpdate(contents=[TextContent(text="chunk1")])
yield ChatResponseUpdate(contents=[TextContent(text="chunk2")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk2")])
execution_order.append("handler_end")
updates: list[ChatResponseUpdate] = []
@@ -730,8 +730,8 @@ class TestChatMiddlewarePipeline:
async def final_handler(ctx: ChatContext) -> AsyncIterable[ChatResponseUpdate]:
execution_order.append("handler_start")
yield ChatResponseUpdate(contents=[TextContent(text="chunk1")])
yield ChatResponseUpdate(contents=[TextContent(text="chunk2")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk1")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk2")])
execution_order.append("handler_end")
updates: list[ChatResponseUpdate] = []
@@ -1264,7 +1264,7 @@ class TestStreamingScenarios:
async def final_stream_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
streaming_flags.append(ctx.is_streaming)
yield AgentResponseUpdate(contents=[TextContent(text="chunk")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk")])
updates: list[AgentResponseUpdate] = []
async for update in pipeline.execute_stream(mock_agent, messages, context_stream, final_stream_handler):
@@ -1292,9 +1292,9 @@ class TestStreamingScenarios:
async def final_stream_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
chunks_processed.append("stream_start")
yield AgentResponseUpdate(contents=[TextContent(text="chunk1")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk1")])
chunks_processed.append("chunk1_yielded")
yield AgentResponseUpdate(contents=[TextContent(text="chunk2")])
yield AgentResponseUpdate(contents=[Content.from_text(text="chunk2")])
chunks_processed.append("chunk2_yielded")
chunks_processed.append("stream_end")
@@ -1342,7 +1342,7 @@ class TestStreamingScenarios:
async def final_stream_handler(ctx: ChatContext) -> AsyncIterable[ChatResponseUpdate]:
streaming_flags.append(ctx.is_streaming)
yield ChatResponseUpdate(contents=[TextContent(text="chunk")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk")])
updates: list[ChatResponseUpdate] = []
async for update in pipeline.execute_stream(
@@ -1371,9 +1371,9 @@ class TestStreamingScenarios:
async def final_stream_handler(ctx: ChatContext) -> AsyncIterable[ChatResponseUpdate]:
chunks_processed.append("stream_start")
yield ChatResponseUpdate(contents=[TextContent(text="chunk1")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk1")])
chunks_processed.append("chunk1_yielded")
yield ChatResponseUpdate(contents=[TextContent(text="chunk2")])
yield ChatResponseUpdate(contents=[Content.from_text(text="chunk2")])
chunks_processed.append("chunk2_yielded")
chunks_processed.append("stream_end")
@@ -1486,7 +1486,7 @@ class TestMiddlewareExecutionControl:
async def final_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
nonlocal handler_called
handler_called = True
yield AgentResponseUpdate(contents=[TextContent(text="should not execute")])
yield AgentResponseUpdate(contents=[Content.from_text(text="should not execute")])
# When middleware doesn't call next(), streaming should yield no updates
updates: list[AgentResponseUpdate] = []
@@ -1617,7 +1617,7 @@ class TestMiddlewareExecutionControl:
async def final_handler(ctx: ChatContext) -> AsyncIterable[ChatResponseUpdate]:
nonlocal handler_called
handler_called = True
yield ChatResponseUpdate(contents=[TextContent(text="should not execute")])
yield ChatResponseUpdate(contents=[Content.from_text(text="should not execute")])
# When middleware doesn't call next(), streaming should yield no updates
updates: list[ChatResponseUpdate] = []
@@ -13,8 +13,8 @@ from agent_framework import (
AgentResponseUpdate,
ChatAgent,
ChatMessage,
Content,
Role,
TextContent,
)
from agent_framework._middleware import (
AgentMiddleware,
@@ -75,8 +75,8 @@ class TestResultOverrideMiddleware:
"""Test that agent middleware can override response for streaming execution."""
async def override_stream() -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(contents=[TextContent(text="overridden")])
yield AgentResponseUpdate(contents=[TextContent(text=" stream")])
yield AgentResponseUpdate(contents=[Content.from_text(text="overridden")])
yield AgentResponseUpdate(contents=[Content.from_text(text=" stream")])
class StreamResponseOverrideMiddleware(AgentMiddleware):
async def process(
@@ -92,7 +92,7 @@ class TestResultOverrideMiddleware:
context = AgentRunContext(agent=mock_agent, messages=messages)
async def final_handler(ctx: AgentRunContext) -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(contents=[TextContent(text="original")])
yield AgentResponseUpdate(contents=[Content.from_text(text="original")])
updates: list[AgentResponseUpdate] = []
async for update in pipeline.execute_stream(mock_agent, messages, context, final_handler):
@@ -175,9 +175,9 @@ class TestResultOverrideMiddleware:
mock_chat_client = MockChatClient()
async def custom_stream() -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(contents=[TextContent(text="Custom")])
yield AgentResponseUpdate(contents=[TextContent(text=" streaming")])
yield AgentResponseUpdate(contents=[TextContent(text=" response!")])
yield AgentResponseUpdate(contents=[Content.from_text(text="Custom")])
yield AgentResponseUpdate(contents=[Content.from_text(text=" streaming")])
yield AgentResponseUpdate(contents=[Content.from_text(text=" response!")])
class ChatAgentStreamOverrideMiddleware(AgentMiddleware):
async def process(
@@ -13,10 +13,8 @@ from agent_framework import (
ChatMiddleware,
ChatResponse,
ChatResponseUpdate,
FunctionCallContent,
FunctionResultContent,
Content,
Role,
TextContent,
agent_middleware,
chat_middleware,
function_middleware,
@@ -201,7 +199,9 @@ class TestChatAgentFunctionBasedMiddleware:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(call_id="test_call", name="test_function", arguments={"text": "test"})
Content.from_function_call(
call_id="test_call", name="test_function", arguments={"text": "test"}
)
],
)
]
@@ -256,7 +256,9 @@ class TestChatAgentFunctionBasedMiddleware:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(call_id="test_call", name="test_function", arguments={"text": "test"})
Content.from_function_call(
call_id="test_call", name="test_function", arguments={"text": "test"}
)
],
)
]
@@ -365,8 +367,8 @@ class TestChatAgentStreamingMiddleware:
# Set up mock streaming responses
chat_client.streaming_responses = [
[
ChatResponseUpdate(contents=[TextContent(text="Streaming")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[TextContent(text=" response")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[Content.from_text(text="Streaming")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[Content.from_text(text=" response")], role=Role.ASSISTANT),
]
]
@@ -550,7 +552,7 @@ class TestChatAgentFunctionMiddlewareWithTools:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_123",
name="sample_tool_function",
arguments='{"location": "Seattle"}',
@@ -585,8 +587,8 @@ class TestChatAgentFunctionMiddlewareWithTools:
# Verify function call and result are in the response
all_contents = [content for message in response.messages for content in message.contents]
function_calls = [c for c in all_contents if isinstance(c, FunctionCallContent)]
function_results = [c for c in all_contents if isinstance(c, FunctionResultContent)]
function_calls = [c for c in all_contents if c.type == "function_call"]
function_results = [c for c in all_contents if c.type == "function_result"]
assert len(function_calls) == 1
assert len(function_results) == 1
@@ -610,7 +612,7 @@ class TestChatAgentFunctionMiddlewareWithTools:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_456",
name="sample_tool_function",
arguments='{"location": "San Francisco"}',
@@ -644,8 +646,8 @@ class TestChatAgentFunctionMiddlewareWithTools:
# Verify function call and result are in the response
all_contents = [content for message in response.messages for content in message.contents]
function_calls = [c for c in all_contents if isinstance(c, FunctionCallContent)]
function_results = [c for c in all_contents if isinstance(c, FunctionResultContent)]
function_calls = [c for c in all_contents if c.type == "function_call"]
function_results = [c for c in all_contents if c.type == "function_result"]
assert len(function_calls) == 1
assert len(function_results) == 1
@@ -682,7 +684,7 @@ class TestChatAgentFunctionMiddlewareWithTools:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_789",
name="sample_tool_function",
arguments='{"location": "New York"}',
@@ -723,8 +725,8 @@ class TestChatAgentFunctionMiddlewareWithTools:
# Verify function call and result are in the response
all_contents = [content for message in response.messages for content in message.contents]
function_calls = [c for c in all_contents if isinstance(c, FunctionCallContent)]
function_results = [c for c in all_contents if isinstance(c, FunctionResultContent)]
function_calls = [c for c in all_contents if c.type == "function_call"]
function_results = [c for c in all_contents if c.type == "function_result"]
assert len(function_calls) == 1
assert len(function_results) == 1
@@ -769,14 +771,16 @@ class TestChatAgentFunctionMiddlewareWithTools:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="test_call", name="sample_tool_function", arguments={"location": "Seattle"}
)
],
)
]
),
ChatResponse(messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent("Function completed")])]),
ChatResponse(
messages=[ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text("Function completed")])]
),
]
# Create ChatAgent with function middleware
@@ -1076,8 +1080,8 @@ class TestRunLevelMiddleware:
# Set up mock streaming responses
chat_client.streaming_responses = [
[
ChatResponseUpdate(contents=[TextContent(text="Stream")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[TextContent(text=" response")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[Content.from_text(text="Stream")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[Content.from_text(text=" response")], role=Role.ASSISTANT),
]
]
@@ -1159,7 +1163,7 @@ class TestRunLevelMiddleware:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="test_call",
name="custom_tool",
arguments='{"message": "test"}',
@@ -1204,8 +1208,8 @@ class TestRunLevelMiddleware:
# Verify function call and result are in the response
all_contents = [content for message in response.messages for content in message.contents]
function_calls = [c for c in all_contents if isinstance(c, FunctionCallContent)]
function_results = [c for c in all_contents if isinstance(c, FunctionResultContent)]
function_calls = [c for c in all_contents if c.type == "function_call"]
function_results = [c for c in all_contents if c.type == "function_result"]
assert len(function_calls) == 1
assert len(function_results) == 1
@@ -1248,7 +1252,7 @@ class TestMiddlewareDecoratorLogic:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="test_call",
name="custom_tool",
arguments='{"message": "test"}',
@@ -1315,7 +1319,7 @@ class TestMiddlewareDecoratorLogic:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="test_call",
name="custom_tool",
arguments='{"message": "test"}',
@@ -1365,7 +1369,7 @@ class TestMiddlewareDecoratorLogic:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="test_call",
name="custom_tool",
arguments='{"message": "test"}',
@@ -1704,8 +1708,8 @@ class TestChatAgentChatMiddleware:
# Set up mock streaming responses
chat_client.streaming_responses = [
[
ChatResponseUpdate(contents=[TextContent(text="Stream")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[TextContent(text=" response")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[Content.from_text(text="Stream")], role=Role.ASSISTANT),
ChatResponseUpdate(contents=[Content.from_text(text=" response")], role=Role.ASSISTANT),
]
]
@@ -1806,7 +1810,7 @@ class TestChatAgentChatMiddleware:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_456",
name="sample_tool_function",
arguments='{"location": "San Francisco"}',
@@ -1850,8 +1854,8 @@ class TestChatAgentChatMiddleware:
# Verify function call and result are in the response
all_contents = [content for message in response.messages for content in message.contents]
function_calls = [c for c in all_contents if isinstance(c, FunctionCallContent)]
function_results = [c for c in all_contents if isinstance(c, FunctionResultContent)]
function_calls = [c for c in all_contents if c.type == "function_call"]
function_results = [c for c in all_contents if c.type == "function_result"]
assert len(function_calls) == 1
assert len(function_results) == 1
@@ -1902,3 +1906,59 @@ class TestChatAgentChatMiddleware:
assert modified_kwargs["max_tokens"] == 500
assert modified_kwargs["new_param"] == "added_by_middleware"
assert modified_kwargs["custom_param"] == "test_value" # Should still be there
class TestMiddlewareWithProtocolOnlyAgent:
"""Test use_agent_middleware with agents implementing only AgentProtocol."""
async def test_middleware_with_protocol_only_agent(self) -> None:
"""Verify middleware works without BaseAgent inheritance for both run and run_stream."""
from collections.abc import AsyncIterable
from agent_framework import AgentProtocol, AgentResponse, AgentResponseUpdate, use_agent_middleware
execution_order: list[str] = []
class TrackingMiddleware(AgentMiddleware):
async def process(
self, context: AgentRunContext, next: Callable[[AgentRunContext], Awaitable[None]]
) -> None:
execution_order.append("before")
await next(context)
execution_order.append("after")
@use_agent_middleware
class ProtocolOnlyAgent:
"""Minimal agent implementing only AgentProtocol, not inheriting from BaseAgent."""
def __init__(self):
self.id = "protocol-only-agent"
self.name = "Protocol Only Agent"
self.description = "Test agent"
self.middleware = [TrackingMiddleware()]
async def run(self, messages=None, *, thread=None, **kwargs) -> AgentResponse:
return AgentResponse(messages=[ChatMessage(role=Role.ASSISTANT, text="response")])
def run_stream(self, messages=None, *, thread=None, **kwargs) -> AsyncIterable[AgentResponseUpdate]:
async def _stream():
yield AgentResponseUpdate()
return _stream()
def get_new_thread(self, **kwargs):
return None
agent = ProtocolOnlyAgent()
assert isinstance(agent, AgentProtocol)
# Test run (non-streaming)
response = await agent.run("test message")
assert response is not None
assert execution_order == ["before", "after"]
# Test run_stream (streaming)
execution_order.clear()
async for _ in agent.run_stream("test message"):
pass
assert execution_order == ["before", "after"]
@@ -9,7 +9,7 @@ from agent_framework import (
ChatMessage,
ChatMiddleware,
ChatResponse,
FunctionCallContent,
Content,
FunctionInvocationContext,
Role,
chat_middleware,
@@ -349,7 +349,7 @@ class TestChatMiddleware:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_1",
name="sample_tool",
arguments={"location": "San Francisco"},
@@ -405,7 +405,7 @@ class TestChatMiddleware:
ChatMessage(
role=Role.ASSISTANT,
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_2",
name="sample_tool",
arguments={"location": "New York"},
@@ -279,6 +279,133 @@ async def test_chat_client_streaming_observability(
assert span.attributes[OtelAttr.OUTPUT_MESSAGES] is not None
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
async def test_chat_client_observability_with_instructions(
mock_chat_client, span_exporter: InMemorySpanExporter, enable_sensitive_data
):
"""Test that system_instructions from options are captured in LLM span."""
import json
client = use_instrumentation(mock_chat_client)()
messages = [ChatMessage(role=Role.USER, text="Test message")]
options = {"model_id": "Test", "instructions": "You are a helpful assistant."}
span_exporter.clear()
response = await client.get_response(messages=messages, options=options)
assert response is not None
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
# Verify system_instructions attribute is set
assert OtelAttr.SYSTEM_INSTRUCTIONS in span.attributes
system_instructions = json.loads(span.attributes[OtelAttr.SYSTEM_INSTRUCTIONS])
assert len(system_instructions) == 1
assert system_instructions[0]["content"] == "You are a helpful assistant."
# Verify input_messages contains system message
input_messages = json.loads(span.attributes[OtelAttr.INPUT_MESSAGES])
assert any(msg.get("role") == "system" for msg in input_messages)
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
async def test_chat_client_streaming_observability_with_instructions(
mock_chat_client, span_exporter: InMemorySpanExporter, enable_sensitive_data
):
"""Test streaming telemetry captures system_instructions from options."""
import json
client = use_instrumentation(mock_chat_client)()
messages = [ChatMessage(role=Role.USER, text="Test")]
options = {"model_id": "Test", "instructions": "You are a helpful assistant."}
span_exporter.clear()
updates = []
async for update in client.get_streaming_response(messages=messages, options=options):
updates.append(update)
assert len(updates) == 2
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
# Verify system_instructions attribute is set
assert OtelAttr.SYSTEM_INSTRUCTIONS in span.attributes
system_instructions = json.loads(span.attributes[OtelAttr.SYSTEM_INSTRUCTIONS])
assert len(system_instructions) == 1
assert system_instructions[0]["content"] == "You are a helpful assistant."
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
async def test_chat_client_observability_without_instructions(
mock_chat_client, span_exporter: InMemorySpanExporter, enable_sensitive_data
):
"""Test that system_instructions attribute is not set when instructions are not provided."""
client = use_instrumentation(mock_chat_client)()
messages = [ChatMessage(role=Role.USER, text="Test message")]
options = {"model_id": "Test"} # No instructions
span_exporter.clear()
response = await client.get_response(messages=messages, options=options)
assert response is not None
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
# Verify system_instructions attribute is NOT set
assert OtelAttr.SYSTEM_INSTRUCTIONS not in span.attributes
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
async def test_chat_client_observability_with_empty_instructions(
mock_chat_client, span_exporter: InMemorySpanExporter, enable_sensitive_data
):
"""Test that system_instructions attribute is not set when instructions is an empty string."""
client = use_instrumentation(mock_chat_client)()
messages = [ChatMessage(role=Role.USER, text="Test message")]
options = {"model_id": "Test", "instructions": ""} # Empty string
span_exporter.clear()
response = await client.get_response(messages=messages, options=options)
assert response is not None
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
# Empty string should not set system_instructions
assert OtelAttr.SYSTEM_INSTRUCTIONS not in span.attributes
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
async def test_chat_client_observability_with_list_instructions(
mock_chat_client, span_exporter: InMemorySpanExporter, enable_sensitive_data
):
"""Test that list-type instructions are correctly captured."""
import json
client = use_instrumentation(mock_chat_client)()
messages = [ChatMessage(role=Role.USER, text="Test message")]
options = {"model_id": "Test", "instructions": ["Instruction 1", "Instruction 2"]}
span_exporter.clear()
response = await client.get_response(messages=messages, options=options)
assert response is not None
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
# Verify system_instructions attribute contains both instructions
assert OtelAttr.SYSTEM_INSTRUCTIONS in span.attributes
system_instructions = json.loads(span.attributes[OtelAttr.SYSTEM_INSTRUCTIONS])
assert len(system_instructions) == 2
assert system_instructions[0]["content"] == "Instruction 1"
assert system_instructions[1]["content"] == "Instruction 2"
async def test_chat_client_without_model_id_observability(mock_chat_client, span_exporter: InMemorySpanExporter):
"""Test telemetry shouldn't fail when the model_id is not provided for unknown reason."""
client = use_instrumentation(mock_chat_client)()
+100 -105
View File
@@ -9,6 +9,7 @@ from pydantic import BaseModel, ValidationError
from agent_framework import (
AIFunction,
Content,
HostedCodeInterpreterTool,
HostedImageGenerationTool,
HostedMCPTool,
@@ -639,24 +640,22 @@ def test_parse_inputs_none():
def test_parse_inputs_string():
"""Test _parse_inputs with string input."""
from agent_framework import UriContent
result = _parse_inputs("http://example.com")
assert len(result) == 1
assert isinstance(result[0], UriContent)
assert result[0].type == "uri"
assert result[0].uri == "http://example.com"
assert result[0].media_type == "text/plain"
def test_parse_inputs_list_of_strings():
"""Test _parse_inputs with list of strings."""
from agent_framework import UriContent
inputs = ["http://example.com", "https://test.org"]
result = _parse_inputs(inputs)
assert len(result) == 2
assert all(isinstance(item, UriContent) for item in result)
assert all(item.type == "uri" for item in result)
assert result[0].uri == "http://example.com"
assert result[1].uri == "https://test.org"
assert all(item.media_type == "text/plain" for item in result)
@@ -664,88 +663,84 @@ def test_parse_inputs_list_of_strings():
def test_parse_inputs_uri_dict():
"""Test _parse_inputs with URI dictionary."""
from agent_framework import UriContent
input_dict = {"uri": "http://example.com", "media_type": "application/json"}
result = _parse_inputs(input_dict)
assert len(result) == 1
assert isinstance(result[0], UriContent)
assert result[0].type == "uri"
assert result[0].uri == "http://example.com"
assert result[0].media_type == "application/json"
def test_parse_inputs_hosted_file_dict():
"""Test _parse_inputs with hosted file dictionary."""
from agent_framework import HostedFileContent
input_dict = {"file_id": "file-123"}
result = _parse_inputs(input_dict)
assert len(result) == 1
assert isinstance(result[0], HostedFileContent)
assert result[0].type == "hosted_file"
assert result[0].file_id == "file-123"
def test_parse_inputs_hosted_vector_store_dict():
"""Test _parse_inputs with hosted vector store dictionary."""
from agent_framework import HostedVectorStoreContent
from agent_framework import Content
input_dict = {"vector_store_id": "vs-789"}
result = _parse_inputs(input_dict)
assert len(result) == 1
assert isinstance(result[0], HostedVectorStoreContent)
assert isinstance(result[0], Content)
assert result[0].type == "hosted_vector_store"
assert result[0].vector_store_id == "vs-789"
def test_parse_inputs_data_dict():
"""Test _parse_inputs with data dictionary."""
from agent_framework import DataContent
input_dict = {"data": b"test data", "media_type": "application/octet-stream"}
result = _parse_inputs(input_dict)
assert len(result) == 1
assert isinstance(result[0], DataContent)
assert result[0].type == "data"
assert result[0].uri == "data:application/octet-stream;base64,dGVzdCBkYXRh"
assert result[0].media_type == "application/octet-stream"
def test_parse_inputs_ai_contents_instance():
"""Test _parse_inputs with Contents instance."""
from agent_framework import TextContent
"""Test _parse_inputs with Content instance."""
text_content = TextContent(text="Hello, world!")
text_content = Content.from_text(text="Hello, world!")
result = _parse_inputs(text_content)
assert len(result) == 1
assert isinstance(result[0], TextContent)
assert result[0].type == "text"
assert result[0].text == "Hello, world!"
def test_parse_inputs_mixed_list():
"""Test _parse_inputs with mixed input types."""
from agent_framework import HostedFileContent, TextContent, UriContent
inputs = [
"http://example.com", # string
{"uri": "https://test.org", "media_type": "text/html"}, # URI dict
{"file_id": "file-456"}, # hosted file dict
TextContent(text="Hello"), # Contents instance
Content.from_text(text="Hello"), # Content instance
]
result = _parse_inputs(inputs)
assert len(result) == 4
assert isinstance(result[0], UriContent)
assert result[0].type == "uri"
assert result[0].uri == "http://example.com"
assert isinstance(result[1], UriContent)
assert result[1].type == "uri"
assert result[1].uri == "https://test.org"
assert result[1].media_type == "text/html"
assert isinstance(result[2], HostedFileContent)
assert result[2].type == "hosted_file"
assert result[2].file_id == "file-456"
assert isinstance(result[3], TextContent)
assert result[3].type == "text"
assert result[3].text == "Hello"
@@ -765,55 +760,51 @@ def test_parse_inputs_unsupported_type():
def test_hosted_code_interpreter_tool_with_string_input():
"""Test HostedCodeInterpreterTool with string input."""
from agent_framework import UriContent
tool = HostedCodeInterpreterTool(inputs="http://example.com")
assert len(tool.inputs) == 1
assert isinstance(tool.inputs[0], UriContent)
assert tool.inputs[0].type == "uri"
assert tool.inputs[0].uri == "http://example.com"
def test_hosted_code_interpreter_tool_with_dict_inputs():
"""Test HostedCodeInterpreterTool with dictionary inputs."""
from agent_framework import HostedFileContent, UriContent
inputs = [{"uri": "http://example.com", "media_type": "text/html"}, {"file_id": "file-123"}]
tool = HostedCodeInterpreterTool(inputs=inputs)
assert len(tool.inputs) == 2
assert isinstance(tool.inputs[0], UriContent)
assert tool.inputs[0].type == "uri"
assert tool.inputs[0].uri == "http://example.com"
assert tool.inputs[0].media_type == "text/html"
assert isinstance(tool.inputs[1], HostedFileContent)
assert tool.inputs[1].type == "hosted_file"
assert tool.inputs[1].file_id == "file-123"
def test_hosted_code_interpreter_tool_with_ai_contents():
"""Test HostedCodeInterpreterTool with Contents instances."""
from agent_framework import DataContent, TextContent
"""Test HostedCodeInterpreterTool with Content instances."""
inputs = [TextContent(text="Hello, world!"), DataContent(data=b"test", media_type="text/plain")]
inputs = [Content.from_text(text="Hello, world!"), Content.from_data(data=b"test", media_type="text/plain")]
tool = HostedCodeInterpreterTool(inputs=inputs)
assert len(tool.inputs) == 2
assert isinstance(tool.inputs[0], TextContent)
assert tool.inputs[0].type == "text"
assert tool.inputs[0].text == "Hello, world!"
assert isinstance(tool.inputs[1], DataContent)
assert tool.inputs[1].type == "data"
assert tool.inputs[1].media_type == "text/plain"
def test_hosted_code_interpreter_tool_with_single_input():
"""Test HostedCodeInterpreterTool with single input (not in list)."""
from agent_framework import HostedFileContent
input_dict = {"file_id": "file-single"}
tool = HostedCodeInterpreterTool(inputs=input_dict)
assert len(tool.inputs) == 1
assert isinstance(tool.inputs[0], HostedFileContent)
assert tool.inputs[0].type == "hosted_file"
assert tool.inputs[0].file_id == "file-single"
@@ -983,7 +974,7 @@ def mock_chat_client():
yield ChatResponseUpdate(contents=[content], role=msg.role)
else:
# Default response
yield ChatResponseUpdate(contents=["Default response"], role="assistant")
yield ChatResponseUpdate(text="Default response", role="assistant")
return MockChatClient()
@@ -1006,7 +997,7 @@ def requires_approval_tool(x: int) -> int:
async def test_non_streaming_single_function_no_approval():
"""Test non-streaming handler with single function call that doesn't require approval."""
from agent_framework import ChatMessage, ChatResponse, FunctionCallContent
from agent_framework import ChatMessage, ChatResponse
from agent_framework._tools import _handle_function_calls_response
# Create mock client
@@ -1017,11 +1008,11 @@ async def test_non_streaming_single_function_no_approval():
messages=[
ChatMessage(
role="assistant",
contents=[FunctionCallContent(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}')],
contents=[Content.from_function_call(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}')],
)
]
)
final_response = ChatResponse(messages=[ChatMessage(role="assistant", contents=["The result is 10"])])
final_response = ChatResponse(messages=[ChatMessage(role="assistant", text="The result is 10")])
call_count = [0]
responses = [initial_response, final_response]
@@ -1039,17 +1030,16 @@ async def test_non_streaming_single_function_no_approval():
# Verify: should have 3 messages: function call, function result, final answer
assert len(result.messages) == 3
assert isinstance(result.messages[0].contents[0], FunctionCallContent)
from agent_framework import FunctionResultContent
assert result.messages[0].contents[0].type == "function_call"
assert isinstance(result.messages[1].contents[0], FunctionResultContent)
assert result.messages[1].contents[0].type == "function_result"
assert result.messages[1].contents[0].result == 10 # 5 * 2
assert result.messages[2].contents[0] == "The result is 10"
assert result.messages[2].text == "The result is 10"
async def test_non_streaming_single_function_requires_approval():
"""Test non-streaming handler with single function call that requires approval."""
from agent_framework import ChatMessage, ChatResponse, FunctionCallContent
from agent_framework import ChatMessage, ChatResponse
from agent_framework._tools import _handle_function_calls_response
mock_client = type("MockClient", (), {})()
@@ -1059,7 +1049,9 @@ async def test_non_streaming_single_function_requires_approval():
messages=[
ChatMessage(
role="assistant",
contents=[FunctionCallContent(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}')],
contents=[
Content.from_function_call(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}')
],
)
]
)
@@ -1078,18 +1070,17 @@ async def test_non_streaming_single_function_requires_approval():
result = await wrapped(mock_client, messages=[], options={"tools": [requires_approval_tool]})
# Verify: should return 1 message with function call and approval request
from agent_framework import FunctionApprovalRequestContent
assert len(result.messages) == 1
assert len(result.messages[0].contents) == 2
assert isinstance(result.messages[0].contents[0], FunctionCallContent)
assert isinstance(result.messages[0].contents[1], FunctionApprovalRequestContent)
assert result.messages[0].contents[0].type == "function_call"
assert result.messages[0].contents[1].type == "function_approval_request"
assert result.messages[0].contents[1].function_call.name == "requires_approval_tool"
async def test_non_streaming_two_functions_both_no_approval():
"""Test non-streaming handler with two function calls, neither requiring approval."""
from agent_framework import ChatMessage, ChatResponse, FunctionCallContent
from agent_framework import ChatMessage, ChatResponse
from agent_framework._tools import _handle_function_calls_response
mock_client = type("MockClient", (), {})()
@@ -1100,15 +1091,13 @@ async def test_non_streaming_two_functions_both_no_approval():
ChatMessage(
role="assistant",
contents=[
FunctionCallContent(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}'),
FunctionCallContent(call_id="call_2", name="no_approval_tool", arguments='{"x": 3}'),
Content.from_function_call(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}'),
Content.from_function_call(call_id="call_2", name="no_approval_tool", arguments='{"x": 3}'),
],
)
]
)
final_response = ChatResponse(
messages=[ChatMessage(role="assistant", contents=["Both tools executed successfully"])]
)
final_response = ChatResponse(messages=[ChatMessage(role="assistant", text="Both tools executed successfully")])
call_count = [0]
responses = [initial_response, final_response]
@@ -1124,21 +1113,20 @@ async def test_non_streaming_two_functions_both_no_approval():
result = await wrapped(mock_client, messages=[], options={"tools": [no_approval_tool]})
# Verify: should have function calls, results, and final answer
from agent_framework import FunctionResultContent
assert len(result.messages) == 3
# First message has both function calls
assert len(result.messages[0].contents) == 2
# Second message has both results
assert len(result.messages[1].contents) == 2
assert all(isinstance(c, FunctionResultContent) for c in result.messages[1].contents)
assert all(c.type == "function_result" for c in result.messages[1].contents)
assert result.messages[1].contents[0].result == 10 # 5 * 2
assert result.messages[1].contents[1].result == 6 # 3 * 2
async def test_non_streaming_two_functions_both_require_approval():
"""Test non-streaming handler with two function calls, both requiring approval."""
from agent_framework import ChatMessage, ChatResponse, FunctionCallContent
from agent_framework import ChatMessage, ChatResponse
from agent_framework._tools import _handle_function_calls_response
mock_client = type("MockClient", (), {})()
@@ -1149,8 +1137,8 @@ async def test_non_streaming_two_functions_both_require_approval():
ChatMessage(
role="assistant",
contents=[
FunctionCallContent(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}'),
FunctionCallContent(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}'),
Content.from_function_call(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}'),
Content.from_function_call(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}'),
],
)
]
@@ -1170,12 +1158,11 @@ async def test_non_streaming_two_functions_both_require_approval():
result = await wrapped(mock_client, messages=[], options={"tools": [requires_approval_tool]})
# Verify: should return 1 message with function calls and approval requests
from agent_framework import FunctionApprovalRequestContent
assert len(result.messages) == 1
assert len(result.messages[0].contents) == 4 # 2 function calls + 2 approval requests
function_calls = [c for c in result.messages[0].contents if isinstance(c, FunctionCallContent)]
approval_requests = [c for c in result.messages[0].contents if isinstance(c, FunctionApprovalRequestContent)]
function_calls = [c for c in result.messages[0].contents if c.type == "function_call"]
approval_requests = [c for c in result.messages[0].contents if c.type == "function_approval_request"]
assert len(function_calls) == 2
assert len(approval_requests) == 2
assert approval_requests[0].function_call.name == "requires_approval_tool"
@@ -1184,7 +1171,7 @@ async def test_non_streaming_two_functions_both_require_approval():
async def test_non_streaming_two_functions_mixed_approval():
"""Test non-streaming handler with two function calls, one requiring approval."""
from agent_framework import ChatMessage, ChatResponse, FunctionCallContent
from agent_framework import ChatMessage, ChatResponse
from agent_framework._tools import _handle_function_calls_response
mock_client = type("MockClient", (), {})()
@@ -1195,8 +1182,8 @@ async def test_non_streaming_two_functions_mixed_approval():
ChatMessage(
role="assistant",
contents=[
FunctionCallContent(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}'),
FunctionCallContent(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}'),
Content.from_function_call(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}'),
Content.from_function_call(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}'),
],
)
]
@@ -1216,17 +1203,16 @@ async def test_non_streaming_two_functions_mixed_approval():
result = await wrapped(mock_client, messages=[], options={"tools": [no_approval_tool, requires_approval_tool]})
# Verify: should return approval requests for both (when one needs approval, all are sent for approval)
from agent_framework import FunctionApprovalRequestContent
assert len(result.messages) == 1
assert len(result.messages[0].contents) == 4 # 2 function calls + 2 approval requests
approval_requests = [c for c in result.messages[0].contents if isinstance(c, FunctionApprovalRequestContent)]
approval_requests = [c for c in result.messages[0].contents if c.type == "function_approval_request"]
assert len(approval_requests) == 2
async def test_streaming_single_function_no_approval():
"""Test streaming handler with single function call that doesn't require approval."""
from agent_framework import ChatResponseUpdate, FunctionCallContent
from agent_framework import ChatResponseUpdate
from agent_framework._tools import _handle_function_calls_streaming_response
mock_client = type("MockClient", (), {})()
@@ -1234,11 +1220,11 @@ async def test_streaming_single_function_no_approval():
# Initial response with function call, then final response after function execution
initial_updates = [
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}')],
contents=[Content.from_function_call(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}')],
role="assistant",
)
]
final_updates = [ChatResponseUpdate(contents=["The result is 10"], role="assistant")]
final_updates = [ChatResponseUpdate(text="The result is 10", role="assistant")]
call_count = [0]
updates_list = [initial_updates, final_updates]
@@ -1257,22 +1243,23 @@ async def test_streaming_single_function_no_approval():
updates.append(update)
# Verify: should have function call update, tool result update (injected), and final update
from agent_framework import FunctionResultContent, Role
from agent_framework import Role
assert len(updates) >= 3
# First update is the function call
assert isinstance(updates[0].contents[0], FunctionCallContent)
assert updates[0].contents[0].type == "function_call"
# Second update should be the tool result (injected by the wrapper)
assert updates[1].role == Role.TOOL
assert isinstance(updates[1].contents[0], FunctionResultContent)
assert updates[1].contents[0].type == "function_result"
assert updates[1].contents[0].result == 10 # 5 * 2
# Last update is the final message
assert updates[-1].contents[0] == "The result is 10"
assert updates[-1].contents[0].type == "text"
assert updates[-1].contents[0].text == "The result is 10"
async def test_streaming_single_function_requires_approval():
"""Test streaming handler with single function call that requires approval."""
from agent_framework import ChatResponseUpdate, FunctionCallContent
from agent_framework import ChatResponseUpdate
from agent_framework._tools import _handle_function_calls_streaming_response
mock_client = type("MockClient", (), {})()
@@ -1280,7 +1267,9 @@ async def test_streaming_single_function_requires_approval():
# Initial response with function call
initial_updates = [
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}')],
contents=[
Content.from_function_call(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}')
],
role="assistant",
)
]
@@ -1302,17 +1291,17 @@ async def test_streaming_single_function_requires_approval():
updates.append(update)
# Verify: should yield function call and then approval request
from agent_framework import FunctionApprovalRequestContent, Role
from agent_framework import Role
assert len(updates) == 2
assert isinstance(updates[0].contents[0], FunctionCallContent)
assert updates[0].contents[0].type == "function_call"
assert updates[1].role == Role.ASSISTANT
assert isinstance(updates[1].contents[0], FunctionApprovalRequestContent)
assert updates[1].contents[0].type == "function_approval_request"
async def test_streaming_two_functions_both_no_approval():
"""Test streaming handler with two function calls, neither requiring approval."""
from agent_framework import ChatResponseUpdate, FunctionCallContent
from agent_framework import ChatResponseUpdate
from agent_framework._tools import _handle_function_calls_streaming_response
mock_client = type("MockClient", (), {})()
@@ -1320,15 +1309,14 @@ async def test_streaming_two_functions_both_no_approval():
# Initial response with two function calls to the same tool
initial_updates = [
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}')],
role="assistant",
),
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_2", name="no_approval_tool", arguments='{"x": 3}')],
contents=[
Content.from_function_call(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}'),
Content.from_function_call(call_id="call_2", name="no_approval_tool", arguments='{"x": 3}'),
],
role="assistant",
),
]
final_updates = [ChatResponseUpdate(contents=["Both tools executed successfully"], role="assistant")]
final_updates = [ChatResponseUpdate(text="Both tools executed successfully", role="assistant")]
call_count = [0]
updates_list = [initial_updates, final_updates]
@@ -1347,22 +1335,23 @@ async def test_streaming_two_functions_both_no_approval():
updates.append(update)
# Verify: should have both function calls, one tool result update with both results, and final message
from agent_framework import FunctionResultContent, Role
from agent_framework import Role
assert len(updates) >= 3
# First two updates are function calls
assert isinstance(updates[0].contents[0], FunctionCallContent)
assert isinstance(updates[1].contents[0], FunctionCallContent)
assert len(updates) >= 2
# First update has both function calls
assert len(updates[0].contents) == 2
assert updates[0].contents[0].type == "function_call"
assert updates[0].contents[1].type == "function_call"
# Should have a tool result update with both results
tool_updates = [u for u in updates if u.role == Role.TOOL]
assert len(tool_updates) == 1
assert len(tool_updates[0].contents) == 2
assert all(isinstance(c, FunctionResultContent) for c in tool_updates[0].contents)
assert all(c.type == "function_result" for c in tool_updates[0].contents)
async def test_streaming_two_functions_both_require_approval():
"""Test streaming handler with two function calls, both requiring approval."""
from agent_framework import ChatResponseUpdate, FunctionCallContent
from agent_framework import ChatResponseUpdate
from agent_framework._tools import _handle_function_calls_streaming_response
mock_client = type("MockClient", (), {})()
@@ -1370,11 +1359,15 @@ async def test_streaming_two_functions_both_require_approval():
# Initial response with two function calls to the same tool
initial_updates = [
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}')],
contents=[
Content.from_function_call(call_id="call_1", name="requires_approval_tool", arguments='{"x": 5}')
],
role="assistant",
),
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}')],
contents=[
Content.from_function_call(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}')
],
role="assistant",
),
]
@@ -1396,20 +1389,20 @@ async def test_streaming_two_functions_both_require_approval():
updates.append(update)
# Verify: should yield both function calls and then approval requests
from agent_framework import FunctionApprovalRequestContent, Role
from agent_framework import Role
assert len(updates) == 3
assert isinstance(updates[0].contents[0], FunctionCallContent)
assert isinstance(updates[1].contents[0], FunctionCallContent)
assert updates[0].contents[0].type == "function_call"
assert updates[1].contents[0].type == "function_call"
# Assistant update with both approval requests
assert updates[2].role == Role.ASSISTANT
assert len(updates[2].contents) == 2
assert all(isinstance(c, FunctionApprovalRequestContent) for c in updates[2].contents)
assert all(c.type == "function_approval_request" for c in updates[2].contents)
async def test_streaming_two_functions_mixed_approval():
"""Test streaming handler with two function calls, one requiring approval."""
from agent_framework import ChatResponseUpdate, FunctionCallContent
from agent_framework import ChatResponseUpdate
from agent_framework._tools import _handle_function_calls_streaming_response
mock_client = type("MockClient", (), {})()
@@ -1417,11 +1410,13 @@ async def test_streaming_two_functions_mixed_approval():
# Initial response with two function calls
initial_updates = [
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}')],
contents=[Content.from_function_call(call_id="call_1", name="no_approval_tool", arguments='{"x": 5}')],
role="assistant",
),
ChatResponseUpdate(
contents=[FunctionCallContent(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}')],
contents=[
Content.from_function_call(call_id="call_2", name="requires_approval_tool", arguments='{"x": 3}')
],
role="assistant",
),
]
@@ -1445,15 +1440,15 @@ async def test_streaming_two_functions_mixed_approval():
updates.append(update)
# Verify: should yield both function calls and then approval requests (when one needs approval, all wait)
from agent_framework import FunctionApprovalRequestContent, Role
from agent_framework import Role
assert len(updates) == 3
assert isinstance(updates[0].contents[0], FunctionCallContent)
assert isinstance(updates[1].contents[0], FunctionCallContent)
assert updates[0].contents[0].type == "function_call"
assert updates[1].contents[0].type == "function_call"
# Assistant update with both approval requests
assert updates[2].role == Role.ASSISTANT
assert len(updates[2].contents) == 2
assert all(isinstance(c, FunctionApprovalRequestContent) for c in updates[2].contents)
assert all(c.type == "function_approval_request" for c in updates[2].contents)
async def test_ai_function_with_kwargs_injection():
File diff suppressed because it is too large Load Diff
@@ -19,15 +19,10 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
FunctionCallContent,
FunctionResultContent,
Content,
HostedCodeInterpreterTool,
HostedFileSearchTool,
HostedVectorStoreContent,
Role,
TextContent,
UriContent,
UsageContent,
ai_function,
)
from agent_framework.exceptions import ServiceInitializationError
@@ -68,7 +63,7 @@ def create_test_openai_assistants_client(
return client
async def create_vector_store(client: OpenAIAssistantsClient) -> tuple[str, HostedVectorStoreContent]:
async def create_vector_store(client: OpenAIAssistantsClient) -> tuple[str, Content]:
"""Create a vector store with sample documents for testing."""
file = await client.client.files.create(
file=("todays_weather.txt", b"The weather today is sunny with a high of 25C."), purpose="user_data"
@@ -81,7 +76,7 @@ async def create_vector_store(client: OpenAIAssistantsClient) -> tuple[str, Host
if result.last_error is not None:
raise Exception(f"Vector store file processing failed with status: {result.last_error.message}")
return file.id, HostedVectorStoreContent(vector_store_id=vector_store.id)
return file.id, Content.from_hosted_vector_store(vector_store_id=vector_store.id)
async def delete_vector_store(client: OpenAIAssistantsClient, file_id: str, vector_store_id: str) -> None:
@@ -464,7 +459,7 @@ async def test_process_stream_events_requires_action(mock_async_openai: MagicMoc
chat_client = create_test_openai_assistants_client(mock_async_openai)
# Mock the _parse_function_calls_from_assistants method to return test content
test_function_content = FunctionCallContent(call_id="call-123", name="test_func", arguments={"arg": "value"})
test_function_content = Content.from_function_call(call_id="call-123", name="test_func", arguments={"arg": "value"})
chat_client._parse_function_calls_from_assistants = MagicMock(return_value=[test_function_content]) # type: ignore
# Create a mock Run object
@@ -578,10 +573,10 @@ async def test_process_stream_events_run_completed_with_usage(
# Check the usage content
usage_content = update.contents[0]
assert isinstance(usage_content, UsageContent)
assert usage_content.details.input_token_count == 100
assert usage_content.details.output_token_count == 50
assert usage_content.details.total_token_count == 150
assert usage_content.type == "usage"
assert usage_content.usage_details["input_token_count"] == 100
assert usage_content.usage_details["output_token_count"] == 50
assert usage_content.usage_details["total_token_count"] == 150
assert update.raw_representation == mock_run
@@ -609,11 +604,86 @@ def test_parse_function_calls_from_assistants_basic(mock_async_openai: MagicMock
# Test that one function call content was created
assert len(contents) == 1
assert isinstance(contents[0], FunctionCallContent)
assert contents[0].type == "function_call"
assert contents[0].name == "get_weather"
assert contents[0].arguments == {"location": "Seattle"}
def test_parse_run_step_with_code_interpreter_tool_call(mock_async_openai: MagicMock) -> None:
"""Test _parse_run_step_tool_call with code_interpreter type creates CodeInterpreterToolCallContent."""
client = create_test_openai_assistants_client(
mock_async_openai,
model_id="test-model",
assistant_id="test-assistant",
)
# Mock a run with required_action containing code_interpreter tool call
mock_run = MagicMock()
mock_run.id = "run_123"
mock_run.status = "requires_action"
mock_tool_call = MagicMock()
mock_tool_call.id = "call_code_123"
mock_tool_call.type = "code_interpreter"
mock_code_interpreter = MagicMock()
mock_code_interpreter.input = "print('Hello, World!')"
mock_tool_call.code_interpreter = mock_code_interpreter
mock_required_action = MagicMock()
mock_required_action.submit_tool_outputs = MagicMock()
mock_required_action.submit_tool_outputs.tool_calls = [mock_tool_call]
mock_run.required_action = mock_required_action
# Parse the run step
contents = client._parse_function_calls_from_assistants(mock_run, "response_123")
# Should have CodeInterpreterToolCallContent
assert len(contents) == 1
assert contents[0].type == "code_interpreter_tool_call"
assert contents[0].call_id == '["response_123", "call_code_123"]'
assert contents[0].inputs is not None
assert len(contents[0].inputs) == 1
assert contents[0].inputs[0].type == "text"
assert contents[0].inputs[0].text == "print('Hello, World!')"
def test_parse_run_step_with_mcp_tool_call(mock_async_openai: MagicMock) -> None:
"""Test _parse_run_step_tool_call with mcp type creates MCPServerToolCallContent."""
client = create_test_openai_assistants_client(
mock_async_openai,
model_id="test-model",
assistant_id="test-assistant",
)
# Mock a run with required_action containing mcp tool call
mock_run = MagicMock()
mock_run.id = "run_456"
mock_run.status = "requires_action"
mock_tool_call = MagicMock()
mock_tool_call.id = "call_mcp_456"
mock_tool_call.type = "mcp"
mock_tool_call.name = "fetch_data"
mock_tool_call.server_label = "DataServer"
mock_tool_call.args = {"key": "value"}
mock_required_action = MagicMock()
mock_required_action.submit_tool_outputs = MagicMock()
mock_required_action.submit_tool_outputs.tool_calls = [mock_tool_call]
mock_run.required_action = mock_required_action
# Parse the run step
contents = client._parse_function_calls_from_assistants(mock_run, "response_456")
# Should have MCPServerToolCallContent
assert len(contents) == 1
assert contents[0].type == "mcp_server_tool_call"
assert contents[0].call_id == '["response_456", "call_mcp_456"]'
assert contents[0].tool_name == "fetch_data"
assert contents[0].server_name == "DataServer"
assert contents[0].arguments == {"key": "value"}
def test_prepare_options_basic(mock_async_openai: MagicMock) -> None:
"""Test _prepare_options with basic chat options."""
chat_client = create_test_openai_assistants_client(mock_async_openai)
@@ -830,7 +900,7 @@ def test_prepare_options_with_image_content(mock_async_openai: MagicMock) -> Non
chat_client = create_test_openai_assistants_client(mock_async_openai)
# Create message with image content
image_content = UriContent(uri="https://example.com/image.jpg", media_type="image/jpeg")
image_content = Content.from_uri(uri="https://example.com/image.jpg", media_type="image/jpeg")
messages = [ChatMessage(role=Role.USER, contents=[image_content])]
# Call the method
@@ -861,7 +931,7 @@ def test_prepare_tool_outputs_for_assistants_valid(mock_async_openai: MagicMock)
chat_client = create_test_openai_assistants_client(mock_async_openai)
call_id = json.dumps(["run-123", "call-456"])
function_result = FunctionResultContent(call_id=call_id, result="Function executed successfully")
function_result = Content.from_function_result(call_id=call_id, result="Function executed successfully")
run_id, tool_outputs = chat_client._prepare_tool_outputs_for_assistants([function_result]) # type: ignore
@@ -881,8 +951,8 @@ def test_prepare_tool_outputs_for_assistants_mismatched_run_ids(
# Create function results with different run IDs
call_id1 = json.dumps(["run-123", "call-456"])
call_id2 = json.dumps(["run-789", "call-xyz"]) # Different run ID
function_result1 = FunctionResultContent(call_id=call_id1, result="Result 1")
function_result2 = FunctionResultContent(call_id=call_id2, result="Result 2")
function_result1 = Content.from_function_result(call_id=call_id1, result="Result 1")
function_result2 = Content.from_function_result(call_id=call_id2, result="Result 2")
run_id, tool_outputs = chat_client._prepare_tool_outputs_for_assistants([function_result1, function_result2]) # type: ignore
@@ -1006,7 +1076,7 @@ async def test_streaming() -> None:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
assert any(word in full_message.lower() for word in ["sunny", "25", "weather", "seattle"])
@@ -1035,7 +1105,7 @@ async def test_streaming_tools() -> None:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
assert any(word in full_message.lower() for word in ["sunny", "25", "weather"])
@@ -1121,7 +1191,7 @@ async def test_file_search_streaming() -> None:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
await delete_vector_store(openai_assistants_client, file_id, vector_store.vector_store_id)
@@ -7,6 +7,8 @@ from unittest.mock import MagicMock, patch
import pytest
from openai import BadRequestError
from openai.types.chat.chat_completion import ChatCompletion, Choice
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from pydantic import BaseModel
from pytest import param
@@ -14,8 +16,7 @@ from agent_framework import (
ChatClientProtocol,
ChatMessage,
ChatResponse,
DataContent,
FunctionResultContent,
Content,
HostedWebSearchTool,
ToolProtocol,
ai_function,
@@ -282,7 +283,9 @@ def test_function_result_falsy_values_handling(openai_unit_test_env: dict[str, s
client = OpenAIChatClient()
# Test with empty list (falsy but not None)
message_with_empty_list = ChatMessage(role="tool", contents=[FunctionResultContent(call_id="call-123", result=[])])
message_with_empty_list = ChatMessage(
role="tool", contents=[Content.from_function_result(call_id="call-123", result=[])]
)
openai_messages = client._prepare_message_for_openai(message_with_empty_list)
assert len(openai_messages) == 1
@@ -290,7 +293,7 @@ def test_function_result_falsy_values_handling(openai_unit_test_env: dict[str, s
# Test with empty string (falsy but not None)
message_with_empty_string = ChatMessage(
role="tool", contents=[FunctionResultContent(call_id="call-456", result="")]
role="tool", contents=[Content.from_function_result(call_id="call-456", result="")]
)
openai_messages = client._prepare_message_for_openai(message_with_empty_string)
@@ -298,7 +301,9 @@ def test_function_result_falsy_values_handling(openai_unit_test_env: dict[str, s
assert openai_messages[0]["content"] == "" # Empty string should be preserved
# Test with False (falsy but not None)
message_with_false = ChatMessage(role="tool", contents=[FunctionResultContent(call_id="call-789", result=False)])
message_with_false = ChatMessage(
role="tool", contents=[Content.from_function_result(call_id="call-789", result=False)]
)
openai_messages = client._prepare_message_for_openai(message_with_false)
assert len(openai_messages) == 1
@@ -317,7 +322,7 @@ def test_function_result_exception_handling(openai_unit_test_env: dict[str, str]
message_with_exception = ChatMessage(
role="tool",
contents=[
FunctionResultContent(call_id="call-123", result="Error: Function failed.", exception=test_exception)
Content.from_function_result(call_id="call-123", result="Error: Function failed.", exception=test_exception)
],
)
@@ -339,7 +344,7 @@ def test_prepare_content_for_openai_data_content_image(openai_unit_test_env: dic
client = OpenAIChatClient()
# Test DataContent with image media type
image_data_content = DataContent(
image_data_content = Content.from_uri(
uri="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==",
media_type="image/png",
)
@@ -351,7 +356,7 @@ def test_prepare_content_for_openai_data_content_image(openai_unit_test_env: dic
assert result["image_url"]["url"] == image_data_content.uri
# Test DataContent with non-image media type should use default model_dump
text_data_content = DataContent(uri="data:text/plain;base64,SGVsbG8gV29ybGQ=", media_type="text/plain")
text_data_content = Content.from_uri(uri="data:text/plain;base64,SGVsbG8gV29ybGQ=", media_type="text/plain")
result = client._prepare_content_for_openai(text_data_content) # type: ignore
@@ -361,7 +366,7 @@ def test_prepare_content_for_openai_data_content_image(openai_unit_test_env: dic
assert result["media_type"] == "text/plain"
# Test DataContent with audio media type
audio_data_content = DataContent(
audio_data_content = Content.from_uri(
uri="data:audio/wav;base64,UklGRjBEAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQwEAAAAAAAAAAAA",
media_type="audio/wav",
)
@@ -375,7 +380,9 @@ def test_prepare_content_for_openai_data_content_image(openai_unit_test_env: dic
assert result["input_audio"]["format"] == "wav"
# Test DataContent with MP3 audio
mp3_data_content = DataContent(uri="data:audio/mp3;base64,//uQAAAAWGluZwAAAA8AAAACAAACcQ==", media_type="audio/mp3")
mp3_data_content = Content.from_uri(
uri="data:audio/mp3;base64,//uQAAAAWGluZwAAAA8AAAACAAACcQ==", media_type="audio/mp3"
)
result = client._prepare_content_for_openai(mp3_data_content) # type: ignore
@@ -391,7 +398,7 @@ def test_prepare_content_for_openai_document_file_mapping(openai_unit_test_env:
client = OpenAIChatClient()
# Test PDF without filename - should omit filename in OpenAI payload
pdf_data_content = DataContent(
pdf_data_content = Content.from_uri(
uri="data:application/pdf;base64,JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFI+PgplbmRvYmoKMiAwIG9iago8PC9UeXBlL1BhZ2VzL0tpZHNbMyAwIFJdL0NvdW50IDE+PgplbmRvYmoKMyAwIG9iago8PC9UeXBlL1BhZ2UvTWVkaWFCb3ggWzAgMCA2MTIgNzkyXS9QYXJlbnQgMiAwIFIvUmVzb3VyY2VzPDwvRm9udDw8L0YxIDQgMCBSPj4+Pi9Db250ZW50cyA1IDAgUj4+CmVuZG9iago0IDAgb2JqCjw8L1R5cGUvRm9udC9TdWJ0eXBlL1R5cGUxL0Jhc2VGb250L0hlbHZldGljYT4+CmVuZG9iago1IDAgb2JqCjw8L0xlbmd0aCA0ND4+CnN0cmVhbQpCVApxCjcwIDUwIFRECi9GMSA4IFRmCihIZWxsbyBXb3JsZCEpIFRqCkVUCmVuZHN0cmVhbQplbmRvYmoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDA5IDAwMDAwIG4gCjAwMDAwMDAwNTggMDAwMDAgbiAKMDAwMDAwMDExNSAwMDAwMCBuIAowMDAwMDAwMjQ1IDAwMDAwIG4gCjAwMDAwMDAzMDcgMDAwMDAgbiAKdHJhaWxlcgo8PC9TaXplIDYvUm9vdCAxIDAgUj4+CnN0YXJ0eHJlZgo0MDUKJSVFT0Y=",
media_type="application/pdf",
)
@@ -407,7 +414,7 @@ def test_prepare_content_for_openai_document_file_mapping(openai_unit_test_env:
assert result["file"]["file_data"] == pdf_data_content.uri
# Test PDF with custom filename via additional_properties
pdf_with_filename = DataContent(
pdf_with_filename = Content.from_uri(
uri="data:application/pdf;base64,JVBERi0xLjQ=",
media_type="application/pdf",
additional_properties={"filename": "report.pdf"},
@@ -441,7 +448,7 @@ def test_prepare_content_for_openai_document_file_mapping(openai_unit_test_env:
for case in test_cases:
# Test without filename
doc_content = DataContent(
doc_content = Content.from_uri(
uri=f"data:{case['media_type']};base64,{case['base64']}",
media_type=case["media_type"],
)
@@ -454,7 +461,7 @@ def test_prepare_content_for_openai_document_file_mapping(openai_unit_test_env:
assert result["file"]["file_data"] == doc_content.uri
# Test with filename - should now use file format with filename
doc_with_filename = DataContent(
doc_with_filename = Content.from_uri(
uri=f"data:{case['media_type']};base64,{case['base64']}",
media_type=case["media_type"],
additional_properties={"filename": case["filename"]},
@@ -468,7 +475,7 @@ def test_prepare_content_for_openai_document_file_mapping(openai_unit_test_env:
assert result["file"]["file_data"] == doc_with_filename.uri
# Test edge case: empty additional_properties dict
pdf_empty_props = DataContent(
pdf_empty_props = Content.from_uri(
uri="data:application/pdf;base64,JVBERi0xLjQ=",
media_type="application/pdf",
additional_properties={},
@@ -480,7 +487,7 @@ def test_prepare_content_for_openai_document_file_mapping(openai_unit_test_env:
assert "filename" not in result["file"]
# Test edge case: None filename in additional_properties
pdf_none_filename = DataContent(
pdf_none_filename = Content.from_uri(
uri="data:application/pdf;base64,JVBERi0xLjQ=",
media_type="application/pdf",
additional_properties={"filename": None},
@@ -492,6 +499,430 @@ def test_prepare_content_for_openai_document_file_mapping(openai_unit_test_env:
assert "filename" not in result["file"] # None filename should be omitted
def test_parse_text_reasoning_content_from_response(openai_unit_test_env: dict[str, str]) -> None:
"""Test that TextReasoningContent is correctly parsed from OpenAI response with reasoning_details."""
client = OpenAIChatClient()
# Mock response with reasoning_details
mock_reasoning_details = {
"effort": "high",
"summary": "Analyzed the problem carefully",
"content": [{"type": "reasoning_text", "text": "Step-by-step thinking..."}],
}
mock_response = ChatCompletion(
id="test-response",
object="chat.completion",
created=1234567890,
model="gpt-5",
choices=[
Choice(
index=0,
message=ChatCompletionMessage(
role="assistant",
content="The answer is 42.",
reasoning_details=mock_reasoning_details,
),
finish_reason="stop",
)
],
)
response = client._parse_response_from_openai(mock_response, {})
# Should have both text and reasoning content
assert len(response.messages) == 1
message = response.messages[0]
assert len(message.contents) == 2
# First should be text content
assert message.contents[0].type == "text"
assert message.contents[0].text == "The answer is 42."
# Second should be reasoning content with protected_data
assert message.contents[1].type == "text_reasoning"
assert message.contents[1].protected_data is not None
parsed_details = json.loads(message.contents[1].protected_data)
assert parsed_details == mock_reasoning_details
def test_parse_text_reasoning_content_from_streaming_chunk(openai_unit_test_env: dict[str, str]) -> None:
"""Test that TextReasoningContent is correctly parsed from streaming OpenAI chunk with reasoning_details."""
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
from openai.types.chat.chat_completion_chunk import Choice as ChunkChoice
from openai.types.chat.chat_completion_chunk import ChoiceDelta as ChunkChoiceDelta
client = OpenAIChatClient()
# Mock streaming chunk with reasoning_details
mock_reasoning_details = {
"type": "reasoning",
"content": "Analyzing the question...",
}
mock_chunk = ChatCompletionChunk(
id="test-chunk",
object="chat.completion.chunk",
created=1234567890,
model="gpt-5",
choices=[
ChunkChoice(
index=0,
delta=ChunkChoiceDelta(
role="assistant",
content="Partial answer",
reasoning_details=mock_reasoning_details,
),
finish_reason=None,
)
],
)
update = client._parse_response_update_from_openai(mock_chunk)
# Should have both text and reasoning content
assert len(update.contents) == 2
# First should be text content
assert update.contents[0].type == "text"
assert update.contents[0].text == "Partial answer"
# Second should be reasoning content
assert update.contents[1].type == "text_reasoning"
assert update.contents[1].protected_data is not None
parsed_details = json.loads(update.contents[1].protected_data)
assert parsed_details == mock_reasoning_details
def test_prepare_message_with_text_reasoning_content(openai_unit_test_env: dict[str, str]) -> None:
"""Test that TextReasoningContent with protected_data is correctly prepared for OpenAI."""
client = OpenAIChatClient()
# Create message with text_reasoning content that has protected_data
# text_reasoning is meant to be added to an existing message, so include text content first
mock_reasoning_data = {
"effort": "medium",
"summary": "Quick analysis",
}
reasoning_content = Content.from_text_reasoning(text=None, protected_data=json.dumps(mock_reasoning_data))
# Message must have other content first for reasoning to attach to
message = ChatMessage(
role="assistant",
contents=[
Content.from_text(text="The answer is 42."),
reasoning_content,
],
)
prepared = client._prepare_message_for_openai(message)
# Should have one message with reasoning_details attached
assert len(prepared) == 1
assert "reasoning_details" in prepared[0]
assert prepared[0]["reasoning_details"] == mock_reasoning_data
# Should also have the text content
assert prepared[0]["content"][0]["type"] == "text"
assert prepared[0]["content"][0]["text"] == "The answer is 42."
def test_function_approval_content_is_skipped_in_preparation(openai_unit_test_env: dict[str, str]) -> None:
"""Test that function approval request and response content are skipped."""
client = OpenAIChatClient()
# Create approval request
function_call = Content.from_function_call(
call_id="call_123",
name="dangerous_action",
arguments='{"confirm": true}',
)
approval_request = Content.from_function_approval_request(
id="approval_001",
function_call=function_call,
)
# Create approval response
approval_response = Content.from_function_approval_response(
approved=False,
id="approval_001",
function_call=function_call,
)
# Test that approval request is skipped
message_with_request = ChatMessage(role="assistant", contents=[approval_request])
prepared_request = client._prepare_message_for_openai(message_with_request)
assert len(prepared_request) == 0 # Should be empty - approval content is skipped
# Test that approval response is skipped
message_with_response = ChatMessage(role="user", contents=[approval_response])
prepared_response = client._prepare_message_for_openai(message_with_response)
assert len(prepared_response) == 0 # Should be empty - approval content is skipped
# Test with mixed content - approval should be skipped, text should remain
mixed_message = ChatMessage(
role="assistant",
contents=[
Content.from_text(text="I need approval for this action."),
approval_request,
],
)
prepared_mixed = client._prepare_message_for_openai(mixed_message)
assert len(prepared_mixed) == 1 # Only text content should remain
assert prepared_mixed[0]["content"][0]["type"] == "text"
assert prepared_mixed[0]["content"][0]["text"] == "I need approval for this action."
def test_usage_content_in_streaming_response(openai_unit_test_env: dict[str, str]) -> None:
"""Test that UsageContent is correctly parsed from streaming response with usage data."""
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
from openai.types.completion_usage import CompletionUsage
client = OpenAIChatClient()
# Mock streaming chunk with usage data (typically last chunk)
mock_usage = CompletionUsage(
prompt_tokens=100,
completion_tokens=50,
total_tokens=150,
)
mock_chunk = ChatCompletionChunk(
id="test-chunk",
object="chat.completion.chunk",
created=1234567890,
model="gpt-4o",
choices=[], # Empty choices when sending usage
usage=mock_usage,
)
update = client._parse_response_update_from_openai(mock_chunk)
# Should have usage content
assert len(update.contents) == 1
assert update.contents[0].type == "usage"
usage_content = update.contents[0]
assert isinstance(usage_content.usage_details, dict)
assert usage_content.usage_details["input_token_count"] == 100
assert usage_content.usage_details["output_token_count"] == 50
assert usage_content.usage_details["total_token_count"] == 150
def test_parse_text_with_refusal(openai_unit_test_env: dict[str, str]) -> None:
"""Test that refusal content is parsed correctly."""
from openai.types.chat.chat_completion import ChatCompletion, Choice
from openai.types.chat.chat_completion_message import ChatCompletionMessage
client = OpenAIChatClient()
# Mock response with refusal
mock_response = ChatCompletion(
id="test-response",
object="chat.completion",
created=1234567890,
model="gpt-4o",
choices=[
Choice(
index=0,
message=ChatCompletionMessage(
role="assistant",
content=None,
refusal="I cannot provide that information.",
),
finish_reason="stop",
)
],
)
response = client._parse_response_from_openai(mock_response, {})
# Should have text content with refusal message
assert len(response.messages) == 1
message = response.messages[0]
assert len(message.contents) == 1
assert message.contents[0].type == "text"
assert message.contents[0].text == "I cannot provide that information."
def test_prepare_options_without_model_id(openai_unit_test_env: dict[str, str]) -> None:
"""Test that prepare_options raises error when model_id is not set."""
client = OpenAIChatClient()
client.model_id = None # Remove model_id
messages = [ChatMessage(role="user", text="test")]
with pytest.raises(ValueError, match="model_id must be a non-empty string"):
client._prepare_options(messages, {})
def test_prepare_options_without_messages(openai_unit_test_env: dict[str, str]) -> None:
"""Test that prepare_options raises error when messages are missing."""
from agent_framework.exceptions import ServiceInvalidRequestError
client = OpenAIChatClient()
with pytest.raises(ServiceInvalidRequestError, match="Messages are required"):
client._prepare_options([], {})
def test_prepare_tools_with_web_search_no_location(openai_unit_test_env: dict[str, str]) -> None:
"""Test preparing web search tool without user location."""
client = OpenAIChatClient()
# Web search tool without additional_properties
web_search_tool = HostedWebSearchTool()
result = client._prepare_tools_for_openai([web_search_tool])
# Should have empty web_search_options (no location)
assert "web_search_options" in result
assert result["web_search_options"] == {}
def test_prepare_options_with_instructions(openai_unit_test_env: dict[str, str]) -> None:
"""Test that instructions are prepended as system message."""
client = OpenAIChatClient()
messages = [ChatMessage(role="user", text="Hello")]
options = {"instructions": "You are a helpful assistant."}
prepared_options = client._prepare_options(messages, options)
# Should have messages with system message prepended
assert "messages" in prepared_options
assert len(prepared_options["messages"]) == 2
assert prepared_options["messages"][0]["role"] == "system"
assert prepared_options["messages"][0]["content"][0]["text"] == "You are a helpful assistant."
def test_prepare_message_with_author_name(openai_unit_test_env: dict[str, str]) -> None:
"""Test that author_name is included in prepared message."""
client = OpenAIChatClient()
message = ChatMessage(
role="user",
author_name="TestUser",
contents=[Content.from_text(text="Hello")],
)
prepared = client._prepare_message_for_openai(message)
assert len(prepared) == 1
assert prepared[0]["name"] == "TestUser"
def test_prepare_message_with_tool_result_author_name(openai_unit_test_env: dict[str, str]) -> None:
"""Test that author_name is not included for TOOL role messages."""
client = OpenAIChatClient()
# Tool messages should not have 'name' field (it's for function name instead)
message = ChatMessage(
role="tool",
author_name="ShouldNotAppear",
contents=[Content.from_function_result(call_id="call_123", result="result")],
)
prepared = client._prepare_message_for_openai(message)
assert len(prepared) == 1
# Should not have 'name' field for tool messages
assert "name" not in prepared[0]
def test_tool_choice_required_with_function_name(openai_unit_test_env: dict[str, str]) -> None:
"""Test that tool_choice with required mode and function name is correctly prepared."""
client = OpenAIChatClient()
messages = [ChatMessage(role="user", text="test")]
options = {
"tools": [get_weather],
"tool_choice": {"mode": "required", "required_function_name": "get_weather"},
}
prepared_options = client._prepare_options(messages, options)
# Should format tool_choice correctly
assert "tool_choice" in prepared_options
assert prepared_options["tool_choice"]["type"] == "function"
assert prepared_options["tool_choice"]["function"]["name"] == "get_weather"
def test_response_format_dict_passthrough(openai_unit_test_env: dict[str, str]) -> None:
"""Test that response_format as dict is passed through directly."""
client = OpenAIChatClient()
messages = [ChatMessage(role="user", text="test")]
custom_format = {
"type": "json_schema",
"json_schema": {"name": "Test", "schema": {"type": "object"}},
}
options = {"response_format": custom_format}
prepared_options = client._prepare_options(messages, options)
# Should pass through the dict directly
assert prepared_options["response_format"] == custom_format
def test_multiple_function_calls_in_single_message(openai_unit_test_env: dict[str, str]) -> None:
"""Test that multiple function calls in a message are correctly prepared."""
client = OpenAIChatClient()
# Create message with multiple function calls
message = ChatMessage(
role="assistant",
contents=[
Content.from_function_call(call_id="call_1", name="func_1", arguments='{"a": 1}'),
Content.from_function_call(call_id="call_2", name="func_2", arguments='{"b": 2}'),
],
)
prepared = client._prepare_message_for_openai(message)
# Should have one message with multiple tool_calls
assert len(prepared) == 1
assert "tool_calls" in prepared[0]
assert len(prepared[0]["tool_calls"]) == 2
assert prepared[0]["tool_calls"][0]["id"] == "call_1"
assert prepared[0]["tool_calls"][1]["id"] == "call_2"
def test_prepare_options_removes_parallel_tool_calls_when_no_tools(openai_unit_test_env: dict[str, str]) -> None:
"""Test that parallel_tool_calls is removed when no tools are present."""
client = OpenAIChatClient()
messages = [ChatMessage(role="user", text="test")]
options = {"allow_multiple_tool_calls": True}
prepared_options = client._prepare_options(messages, options)
# Should not have parallel_tool_calls when no tools
assert "parallel_tool_calls" not in prepared_options
async def test_streaming_exception_handling(openai_unit_test_env: dict[str, str]) -> None:
"""Test that streaming errors are properly handled."""
client = OpenAIChatClient()
messages = [ChatMessage(role="user", text="test")]
# Create a mock error during streaming
mock_error = Exception("Streaming error")
with (
patch.object(client.client.chat.completions, "create", side_effect=mock_error),
pytest.raises(ServiceResponseException),
):
async def consume_stream():
async for _ in client._inner_get_streaming_response(messages=messages, options={}): # type: ignore
pass
await consume_stream()
# region Integration Tests
@@ -33,26 +33,13 @@ from agent_framework import (
ChatOptions,
ChatResponse,
ChatResponseUpdate,
CodeInterpreterToolCallContent,
CodeInterpreterToolResultContent,
DataContent,
FunctionApprovalRequestContent,
FunctionApprovalResponseContent,
FunctionCallContent,
FunctionResultContent,
Content,
HostedCodeInterpreterTool,
HostedFileContent,
HostedFileSearchTool,
HostedImageGenerationTool,
HostedMCPTool,
HostedVectorStoreContent,
HostedWebSearchTool,
ImageGenerationToolCallContent,
ImageGenerationToolResultContent,
Role,
TextContent,
TextReasoningContent,
UriContent,
ai_function,
)
from agent_framework.exceptions import (
@@ -81,7 +68,7 @@ class OutputStruct(BaseModel):
async def create_vector_store(
client: OpenAIResponsesClient,
) -> tuple[str, HostedVectorStoreContent]:
) -> tuple[str, Content]:
"""Create a vector store with sample documents for testing."""
file = await client.client.files.create(
file=("todays_weather.txt", b"The weather today is sunny with a high of 75F."),
@@ -99,7 +86,7 @@ async def create_vector_store(
if result.last_error is not None:
raise Exception(f"Vector store file processing failed with status: {result.last_error.message}")
return file.id, HostedVectorStoreContent(vector_store_id=vector_store.id)
return file.id, Content.from_hosted_vector_store(vector_store_id=vector_store.id)
async def delete_vector_store(client: OpenAIResponsesClient, file_id: str, vector_store_id: str) -> None:
@@ -285,7 +272,7 @@ def test_file_search_tool_with_invalid_inputs() -> None:
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Test with invalid inputs type (should trigger ValueError)
file_search_tool = HostedFileSearchTool(inputs=[HostedFileContent(file_id="invalid")])
file_search_tool = HostedFileSearchTool(inputs=[Content.from_hosted_file(file_id="invalid")])
# Should raise an error due to invalid inputs
with pytest.raises(ValueError, match="HostedFileSearchTool requires inputs to be of type"):
@@ -314,7 +301,7 @@ def test_code_interpreter_tool_variations() -> None:
# Test code interpreter with files
code_tool_with_files = HostedCodeInterpreterTool(
inputs=[HostedFileContent(file_id="file1"), HostedFileContent(file_id="file2")]
inputs=[Content.from_hosted_file(file_id="file1"), Content.from_hosted_file(file_id="file2")]
)
with pytest.raises(ServiceResponseException):
@@ -367,14 +354,14 @@ def test_chat_message_parsing_with_function_calls() -> None:
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Create messages with function call and result content
function_call = FunctionCallContent(
function_call = Content.from_function_call(
call_id="test-call-id",
name="test_function",
arguments='{"param": "value"}',
additional_properties={"fc_id": "test-fc-id"},
)
function_result = FunctionResultContent(call_id="test-call-id", result="Function executed successfully")
function_result = Content.from_function_result(call_id="test-call-id", result="Function executed successfully")
messages = [
ChatMessage(role="user", text="Call a function"),
@@ -516,7 +503,7 @@ def test_response_content_creation_with_annotations() -> None:
response = client._parse_response_from_openai(mock_response, options={}) # type: ignore
assert len(response.messages[0].contents) >= 1
assert isinstance(response.messages[0].contents[0], TextContent)
assert response.messages[0].contents[0].type == "text"
assert response.messages[0].contents[0].text == "Text with annotations."
assert response.messages[0].contents[0].annotations is not None
@@ -547,7 +534,7 @@ def test_response_content_creation_with_refusal() -> None:
response = client._parse_response_from_openai(mock_response, options={}) # type: ignore
assert len(response.messages[0].contents) == 1
assert isinstance(response.messages[0].contents[0], TextContent)
assert response.messages[0].contents[0].type == "text"
assert response.messages[0].contents[0].text == "I cannot provide that information."
@@ -577,7 +564,7 @@ def test_response_content_creation_with_reasoning() -> None:
response = client._parse_response_from_openai(mock_response, options={}) # type: ignore
assert len(response.messages[0].contents) == 2
assert isinstance(response.messages[0].contents[0], TextReasoningContent)
assert response.messages[0].contents[0].type == "text_reasoning"
assert response.messages[0].contents[0].text == "Reasoning step"
@@ -614,13 +601,13 @@ def test_response_content_creation_with_code_interpreter() -> None:
assert len(response.messages[0].contents) == 2
call_content, result_content = response.messages[0].contents
assert isinstance(call_content, CodeInterpreterToolCallContent)
assert call_content.type == "code_interpreter_tool_call"
assert call_content.inputs is not None
assert isinstance(call_content.inputs[0], TextContent)
assert isinstance(result_content, CodeInterpreterToolResultContent)
assert call_content.inputs[0].type == "text"
assert result_content.type == "code_interpreter_tool_result"
assert result_content.outputs is not None
assert any(isinstance(out, TextContent) for out in result_content.outputs)
assert any(isinstance(out, UriContent) for out in result_content.outputs)
assert any(out.type == "text" for out in result_content.outputs)
assert any(out.type == "uri" for out in result_content.outputs)
def test_response_content_creation_with_function_call() -> None:
@@ -648,13 +635,460 @@ def test_response_content_creation_with_function_call() -> None:
response = client._parse_response_from_openai(mock_response, options={}) # type: ignore
assert len(response.messages[0].contents) == 1
assert isinstance(response.messages[0].contents[0], FunctionCallContent)
assert response.messages[0].contents[0].type == "function_call"
function_call = response.messages[0].contents[0]
assert function_call.call_id == "call_123"
assert function_call.name == "get_weather"
assert function_call.arguments == '{"location": "Seattle"}'
def test_prepare_content_for_openai_function_approval_response() -> None:
"""Test _prepare_content_for_openai with function approval response content."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Test approved response
function_call = Content.from_function_call(
call_id="call_123",
name="send_email",
arguments='{"to": "user@example.com"}',
)
approval_response = Content.from_function_approval_response(
approved=True,
id="approval_001",
function_call=function_call,
)
result = client._prepare_content_for_openai(Role.ASSISTANT, approval_response, {})
assert result["type"] == "mcp_approval_response"
assert result["approval_request_id"] == "approval_001"
assert result["approve"] is True
def test_prepare_content_for_openai_error_content() -> None:
"""Test _prepare_content_for_openai with error content."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
error_content = Content.from_error(
message="Operation failed",
error_code="ERR_123",
error_details="Invalid parameter",
)
result = client._prepare_content_for_openai(Role.ASSISTANT, error_content, {})
# ErrorContent should return empty dict (logged but not sent)
assert result == {}
def test_prepare_content_for_openai_usage_content() -> None:
"""Test _prepare_content_for_openai with usage content."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
usage_content = Content.from_usage(
usage_details={
"input_token_count": 100,
"output_token_count": 50,
"total_token_count": 150,
}
)
result = client._prepare_content_for_openai(Role.ASSISTANT, usage_content, {})
# UsageContent should return empty dict (logged but not sent)
assert result == {}
def test_prepare_content_for_openai_hosted_vector_store_content() -> None:
"""Test _prepare_content_for_openai with hosted vector store content."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
vector_store_content = Content.from_hosted_vector_store(
vector_store_id="vs_123",
)
result = client._prepare_content_for_openai(Role.ASSISTANT, vector_store_content, {})
# HostedVectorStoreContent should return empty dict (logged but not sent)
assert result == {}
def test_parse_response_from_openai_with_mcp_server_tool_result() -> None:
"""Test _parse_response_from_openai with MCP server tool result."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
mock_response = MagicMock()
mock_response.output_parsed = None
mock_response.metadata = {}
mock_response.usage = None
mock_response.id = "resp-id"
mock_response.model = "test-model"
mock_response.created_at = 1000000000
# Mock MCP call item with result
mock_mcp_item = MagicMock()
mock_mcp_item.type = "mcp_call"
mock_mcp_item.id = "mcp_call_123"
mock_mcp_item.name = "get_data"
mock_mcp_item.arguments = {"key": "value"}
mock_mcp_item.server_label = "TestServer"
mock_mcp_item.result = [{"content": [{"type": "text", "text": "MCP result"}]}]
mock_response.output = [mock_mcp_item]
response = client._parse_response_from_openai(mock_response, options={}) # type: ignore
# Should have both call and result content
assert len(response.messages[0].contents) == 2
call_content, result_content = response.messages[0].contents
assert call_content.type == "mcp_server_tool_call"
assert call_content.call_id == "mcp_call_123"
assert call_content.tool_name == "get_data"
assert call_content.server_name == "TestServer"
assert result_content.type == "mcp_server_tool_result"
assert result_content.call_id == "mcp_call_123"
assert result_content.output is not None
def test_parse_chunk_from_openai_with_mcp_call_result() -> None:
"""Test _parse_chunk_from_openai with MCP call output."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Mock event with MCP call that has output
mock_event = MagicMock()
mock_event.type = "response.output_item.added"
mock_item = MagicMock()
mock_item.type = "mcp_call"
mock_item.id = "mcp_call_456"
mock_item.call_id = "call_456"
mock_item.name = "fetch_resource"
mock_item.server_label = "ResourceServer"
mock_item.arguments = {"resource_id": "123"}
# Use proper content structure that _parse_content can handle
mock_item.result = [{"type": "text", "text": "test result"}]
mock_event.item = mock_item
mock_event.output_index = 0
function_call_ids: dict[int, tuple[str, str]] = {}
update = client._parse_chunk_from_openai(mock_event, options={}, function_call_ids=function_call_ids)
# Should have both call and result in contents
assert len(update.contents) == 2
call_content, result_content = update.contents
assert call_content.type == "mcp_server_tool_call"
assert call_content.call_id in ["mcp_call_456", "call_456"]
assert call_content.tool_name == "fetch_resource"
assert result_content.type == "mcp_server_tool_result"
assert result_content.call_id in ["mcp_call_456", "call_456"]
# Verify the output was parsed
assert result_content.output is not None
def test_prepare_message_for_openai_with_function_approval_response() -> None:
"""Test _prepare_message_for_openai with function approval response content in messages."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
function_call = Content.from_function_call(
call_id="call_789",
name="execute_command",
arguments='{"command": "ls"}',
)
approval_response = Content.from_function_approval_response(
approved=True,
id="approval_003",
function_call=function_call,
)
message = ChatMessage(role="user", contents=[approval_response])
call_id_to_id: dict[str, str] = {}
result = client._prepare_message_for_openai(message, call_id_to_id)
# FunctionApprovalResponseContent is added directly, not nested in args with role
assert len(result) == 1
prepared_message = result[0]
assert prepared_message["type"] == "mcp_approval_response"
assert prepared_message["approval_request_id"] == "approval_003"
assert prepared_message["approve"] is True
def test_chat_message_with_error_content() -> None:
"""Test that error content in messages is handled properly."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
error_content = Content.from_error(
message="Test error",
error_code="TEST_ERR",
)
message = ChatMessage(role="assistant", contents=[error_content])
call_id_to_id: dict[str, str] = {}
result = client._prepare_message_for_openai(message, call_id_to_id)
# Message should be prepared with empty content list since ErrorContent returns {}
assert len(result) == 1
prepared_message = result[0]
assert prepared_message["role"] == "assistant"
# Content should be a list with empty dict since ErrorContent returns {}
assert prepared_message.get("content") == [{}]
def test_chat_message_with_usage_content() -> None:
"""Test that usage content in messages is handled properly."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
usage_content = Content.from_usage(
usage_details={
"input_token_count": 200,
"output_token_count": 100,
"total_token_count": 300,
}
)
message = ChatMessage(role="assistant", contents=[usage_content])
call_id_to_id: dict[str, str] = {}
result = client._prepare_message_for_openai(message, call_id_to_id)
# Message should be prepared with empty content list since UsageContent returns {}
assert len(result) == 1
prepared_message = result[0]
assert prepared_message["role"] == "assistant"
# Content should be a list with empty dict since UsageContent returns {}
assert prepared_message.get("content") == [{}]
def test_hosted_file_content_preparation() -> None:
"""Test _prepare_content_for_openai with hosted file content."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
hosted_file = Content.from_hosted_file(
file_id="file_abc123",
media_type="application/pdf",
name="document.pdf",
)
result = client._prepare_content_for_openai(Role.USER, hosted_file, {})
assert result["type"] == "input_file"
assert result["file_id"] == "file_abc123"
def test_function_approval_response_with_mcp_tool_call() -> None:
"""Test function approval response content with MCP server tool call content."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
mcp_call = Content.from_mcp_server_tool_call(
call_id="mcp_call_999",
tool_name="sensitive_action",
server_name="SecureServer",
arguments={"action": "delete"},
)
approval_response = Content.from_function_approval_response(
approved=False,
id="approval_mcp_001",
function_call=mcp_call,
)
result = client._prepare_content_for_openai(Role.ASSISTANT, approval_response, {})
assert result["type"] == "mcp_approval_response"
assert result["approval_request_id"] == "approval_mcp_001"
assert result["approve"] is False
def test_response_format_with_conflicting_definitions() -> None:
"""Test that conflicting response_format definitions raise an error."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Mock response_format and text_config that conflict
response_format = {"type": "json_schema", "format": {"type": "json_schema", "name": "Test", "schema": {}}}
text_config = {"format": {"type": "json_object"}}
with pytest.raises(ServiceInvalidRequestError, match="Conflicting response_format definitions"):
client._prepare_response_and_text_format(response_format=response_format, text_config=text_config)
def test_response_format_json_object_type() -> None:
"""Test response_format with json_object type."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {"type": "json_object"}
_, text_config = client._prepare_response_and_text_format(response_format=response_format, text_config=None)
assert text_config is not None
assert text_config["format"]["type"] == "json_object"
def test_response_format_text_type() -> None:
"""Test response_format with text type."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {"type": "text"}
_, text_config = client._prepare_response_and_text_format(response_format=response_format, text_config=None)
assert text_config is not None
assert text_config["format"]["type"] == "text"
def test_response_format_with_format_key() -> None:
"""Test response_format that already has a format key."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {"format": {"type": "json_schema", "name": "MySchema", "schema": {"type": "object"}}}
_, text_config = client._prepare_response_and_text_format(response_format=response_format, text_config=None)
assert text_config is not None
assert text_config["format"]["type"] == "json_schema"
assert text_config["format"]["name"] == "MySchema"
def test_response_format_json_schema_no_name_uses_title() -> None:
"""Test json_schema response_format without name uses title from schema."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {
"type": "json_schema",
"json_schema": {"schema": {"title": "MyTitle", "type": "object", "properties": {}}},
}
_, text_config = client._prepare_response_and_text_format(response_format=response_format, text_config=None)
assert text_config is not None
assert text_config["format"]["name"] == "MyTitle"
def test_response_format_json_schema_with_strict() -> None:
"""Test json_schema response_format with strict mode."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {
"type": "json_schema",
"json_schema": {"name": "StrictSchema", "schema": {"type": "object"}, "strict": True},
}
_, text_config = client._prepare_response_and_text_format(response_format=response_format, text_config=None)
assert text_config is not None
assert text_config["format"]["strict"] is True
def test_response_format_json_schema_with_description() -> None:
"""Test json_schema response_format with description."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {
"type": "json_schema",
"json_schema": {
"name": "DescribedSchema",
"schema": {"type": "object"},
"description": "A test schema",
},
}
_, text_config = client._prepare_response_and_text_format(response_format=response_format, text_config=None)
assert text_config is not None
assert text_config["format"]["description"] == "A test schema"
def test_response_format_json_schema_missing_schema() -> None:
"""Test json_schema response_format without schema raises error."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {"type": "json_schema", "json_schema": {"name": "NoSchema"}}
with pytest.raises(ServiceInvalidRequestError, match="json_schema response_format requires a schema"):
client._prepare_response_and_text_format(response_format=response_format, text_config=None)
def test_response_format_unsupported_type() -> None:
"""Test unsupported response_format type raises error."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = {"type": "unsupported_format"}
with pytest.raises(ServiceInvalidRequestError, match="Unsupported response_format"):
client._prepare_response_and_text_format(response_format=response_format, text_config=None)
def test_response_format_invalid_type() -> None:
"""Test invalid response_format type raises error."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
response_format = "invalid" # Not a Pydantic model or mapping
with pytest.raises(ServiceInvalidRequestError, match="response_format must be a Pydantic model or mapping"):
client._prepare_response_and_text_format(response_format=response_format, text_config=None) # type: ignore
def test_parse_response_with_store_false() -> None:
"""Test _get_conversation_id returns None when store is False."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
mock_response = MagicMock()
mock_response.id = "resp_123"
mock_response.conversation = MagicMock()
mock_response.conversation.id = "conv_456"
conversation_id = client._get_conversation_id(mock_response, store=False)
assert conversation_id is None
def test_parse_response_uses_response_id_when_no_conversation() -> None:
"""Test _get_conversation_id returns response ID when no conversation exists."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
mock_response = MagicMock()
mock_response.id = "resp_789"
mock_response.conversation = None
conversation_id = client._get_conversation_id(mock_response, store=True)
assert conversation_id == "resp_789"
def test_streaming_chunk_with_usage_only() -> None:
"""Test streaming chunk that only contains usage info."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
chat_options = ChatOptions()
function_call_ids: dict[int, tuple[str, str]] = {}
mock_event = MagicMock()
mock_event.type = "response.completed"
mock_event.response = MagicMock()
mock_event.response.id = "resp_usage"
mock_event.response.model = "test-model"
mock_event.response.conversation = None
mock_event.response.usage = MagicMock()
mock_event.response.usage.input_tokens = 50
mock_event.response.usage.output_tokens = 25
mock_event.response.usage.total_tokens = 75
mock_event.response.usage.input_tokens_details = None
mock_event.response.usage.output_tokens_details = None
update = client._parse_chunk_from_openai(mock_event, chat_options, function_call_ids)
# Should have usage content
assert len(update.contents) == 1
assert update.contents[0].type == "usage"
assert update.contents[0].usage_details["total_token_count"] == 75
def test_prepare_tools_for_openai_with_hosted_mcp() -> None:
"""Test that HostedMCPTool is converted to the correct response tool dict."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
@@ -708,7 +1142,7 @@ def test_parse_response_from_openai_with_mcp_approval_request() -> None:
response = client._parse_response_from_openai(mock_response, options={}) # type: ignore
assert isinstance(response.messages[0].contents[0], FunctionApprovalRequestContent)
assert response.messages[0].contents[0].type == "function_approval_request"
req = response.messages[0].contents[0]
assert req.id == "approval-1"
assert req.function_call.name == "do_sensitive_action"
@@ -874,8 +1308,8 @@ def test_parse_chunk_from_openai_with_mcp_approval_request() -> None:
mock_event.item = mock_item
update = client._parse_chunk_from_openai(mock_event, chat_options, function_call_ids)
assert any(isinstance(c, FunctionApprovalRequestContent) for c in update.contents)
fa = next(c for c in update.contents if isinstance(c, FunctionApprovalRequestContent))
assert any(c.type == "function_approval_request" for c in update.contents)
fa = next(c for c in update.contents if c.type == "function_approval_request")
assert fa.id == "approval-stream-1"
assert fa.function_call.name == "do_stream_action"
@@ -925,12 +1359,12 @@ async def test_end_to_end_mcp_approval_flow(span_exporter) -> None:
with patch.object(client.client.responses, "create", side_effect=[mock_response1, mock_response2]) as mock_create:
# First call: get the approval request
response = await client.get_response(messages=[ChatMessage(role="user", text="Trigger approval")])
assert isinstance(response.messages[0].contents[0], FunctionApprovalRequestContent)
assert response.messages[0].contents[0].type == "function_approval_request"
req = response.messages[0].contents[0]
assert req.id == "approval-1"
# Build a user approval and send it (include required function_call)
approval = FunctionApprovalResponseContent(approved=True, id=req.id, function_call=req.function_call)
approval = Content.from_function_approval_response(approved=True, id=req.id, function_call=req.function_call)
approval_message = ChatMessage(role="user", contents=[approval])
_ = await client.get_response(messages=[approval_message])
@@ -961,9 +1395,9 @@ def test_usage_details_basic() -> None:
details = client._parse_usage_from_openai(mock_usage) # type: ignore
assert details is not None
assert details.input_token_count == 100
assert details.output_token_count == 50
assert details.total_token_count == 150
assert details["input_token_count"] == 100
assert details["output_token_count"] == 50
assert details["total_token_count"] == 150
def test_usage_details_with_cached_tokens() -> None:
@@ -980,8 +1414,8 @@ def test_usage_details_with_cached_tokens() -> None:
details = client._parse_usage_from_openai(mock_usage) # type: ignore
assert details is not None
assert details.input_token_count == 200
assert details.additional_counts["openai.cached_input_tokens"] == 25
assert details["input_token_count"] == 200
assert details["openai.cached_input_tokens"] == 25
def test_usage_details_with_reasoning_tokens() -> None:
@@ -998,8 +1432,8 @@ def test_usage_details_with_reasoning_tokens() -> None:
details = client._parse_usage_from_openai(mock_usage) # type: ignore
assert details is not None
assert details.output_token_count == 80
assert details.additional_counts["openai.reasoning_tokens"] == 30
assert details["output_token_count"] == 80
assert details["openai.reasoning_tokens"] == 30
def test_get_metadata_from_response() -> None:
@@ -1098,7 +1532,7 @@ def test_streaming_annotation_added_with_file_path() -> None:
assert len(response.contents) == 1
content = response.contents[0]
assert isinstance(content, HostedFileContent)
assert content.type == "hosted_file"
assert content.file_id == "file-abc123"
assert content.additional_properties is not None
assert content.additional_properties.get("annotation_index") == 0
@@ -1125,7 +1559,7 @@ def test_streaming_annotation_added_with_file_citation() -> None:
assert len(response.contents) == 1
content = response.contents[0]
assert isinstance(content, HostedFileContent)
assert content.type == "hosted_file"
assert content.file_id == "file-xyz789"
assert content.additional_properties is not None
assert content.additional_properties.get("filename") == "sample.txt"
@@ -1154,7 +1588,7 @@ def test_streaming_annotation_added_with_container_file_citation() -> None:
assert len(response.contents) == 1
content = response.contents[0]
assert isinstance(content, HostedFileContent)
assert content.type == "hosted_file"
assert content.file_id == "file-container123"
assert content.additional_properties is not None
assert content.additional_properties.get("container_id") == "container-456"
@@ -1228,7 +1662,7 @@ def test_prepare_content_for_openai_image_content() -> None:
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Test image content with detail parameter and file_id
image_content_with_detail = UriContent(
image_content_with_detail = Content.from_uri(
uri="https://example.com/image.jpg",
media_type="image/jpeg",
additional_properties={"detail": "high", "file_id": "file_123"},
@@ -1240,7 +1674,7 @@ def test_prepare_content_for_openai_image_content() -> None:
assert result["file_id"] == "file_123"
# Test image content without additional properties (defaults)
image_content_basic = UriContent(uri="https://example.com/basic.png", media_type="image/png")
image_content_basic = Content.from_uri(uri="https://example.com/basic.png", media_type="image/png")
result = client._prepare_content_for_openai(Role.USER, image_content_basic, {}) # type: ignore
assert result["type"] == "input_image"
assert result["detail"] == "auto"
@@ -1252,14 +1686,14 @@ def test_prepare_content_for_openai_audio_content() -> None:
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Test WAV audio content
wav_content = UriContent(uri="data:audio/wav;base64,abc123", media_type="audio/wav")
wav_content = Content.from_uri(uri="data:audio/wav;base64,abc123", media_type="audio/wav")
result = client._prepare_content_for_openai(Role.USER, wav_content, {}) # type: ignore
assert result["type"] == "input_audio"
assert result["input_audio"]["data"] == "data:audio/wav;base64,abc123"
assert result["input_audio"]["format"] == "wav"
# Test MP3 audio content
mp3_content = UriContent(uri="data:audio/mp3;base64,def456", media_type="audio/mp3")
mp3_content = Content.from_uri(uri="data:audio/mp3;base64,def456", media_type="audio/mp3")
result = client._prepare_content_for_openai(Role.USER, mp3_content, {}) # type: ignore
assert result["type"] == "input_audio"
assert result["input_audio"]["format"] == "mp3"
@@ -1270,12 +1704,12 @@ def test_prepare_content_for_openai_unsupported_content() -> None:
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Test unsupported audio format
unsupported_audio = UriContent(uri="data:audio/ogg;base64,ghi789", media_type="audio/ogg")
unsupported_audio = Content.from_uri(uri="data:audio/ogg;base64,ghi789", media_type="audio/ogg")
result = client._prepare_content_for_openai(Role.USER, unsupported_audio, {}) # type: ignore
assert result == {}
# Test non-media content
text_uri_content = UriContent(uri="https://example.com/document.txt", media_type="text/plain")
text_uri_content = Content.from_uri(uri="https://example.com/document.txt", media_type="text/plain")
result = client._prepare_content_for_openai(Role.USER, text_uri_content, {}) # type: ignore
assert result == {}
@@ -1299,11 +1733,9 @@ def test_parse_chunk_from_openai_code_interpreter() -> None:
result = client._parse_chunk_from_openai(mock_event_image, chat_options, function_call_ids) # type: ignore
assert len(result.contents) == 1
assert isinstance(result.contents[0], CodeInterpreterToolResultContent)
assert result.contents[0].type == "code_interpreter_tool_result"
assert result.contents[0].outputs
assert any(
isinstance(out, UriContent) and out.uri == "https://example.com/plot.png" for out in result.contents[0].outputs
)
assert any(out.type == "uri" and out.uri == "https://example.com/plot.png" for out in result.contents[0].outputs)
def test_parse_chunk_from_openai_reasoning() -> None:
@@ -1324,7 +1756,7 @@ def test_parse_chunk_from_openai_reasoning() -> None:
result = client._parse_chunk_from_openai(mock_event_reasoning, chat_options, function_call_ids) # type: ignore
assert len(result.contents) == 1
assert isinstance(result.contents[0], TextReasoningContent)
assert result.contents[0].type == "text_reasoning"
assert result.contents[0].text == "Analyzing the problem step by step..."
if result.contents[0].additional_properties:
assert result.contents[0].additional_properties["summary"] == "Problem analysis summary"
@@ -1335,7 +1767,7 @@ def test_prepare_content_for_openai_text_reasoning_comprehensive() -> None:
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
# Test TextReasoningContent with all additional properties
comprehensive_reasoning = TextReasoningContent(
comprehensive_reasoning = Content.from_text_reasoning(
text="Comprehensive reasoning summary",
additional_properties={
"status": "in_progress",
@@ -1371,7 +1803,7 @@ def test_streaming_reasoning_text_delta_event() -> None:
response = client._parse_chunk_from_openai(event, chat_options, function_call_ids) # type: ignore
assert len(response.contents) == 1
assert isinstance(response.contents[0], TextReasoningContent)
assert response.contents[0].type == "text_reasoning"
assert response.contents[0].text == "reasoning delta"
assert response.contents[0].raw_representation == event
mock_metadata.assert_called_once_with(event)
@@ -1396,7 +1828,7 @@ def test_streaming_reasoning_text_done_event() -> None:
response = client._parse_chunk_from_openai(event, chat_options, function_call_ids) # type: ignore
assert len(response.contents) == 1
assert isinstance(response.contents[0], TextReasoningContent)
assert response.contents[0].type == "text_reasoning"
assert response.contents[0].text == "complete reasoning"
assert response.contents[0].raw_representation == event
mock_metadata.assert_called_once_with(event)
@@ -1422,7 +1854,7 @@ def test_streaming_reasoning_summary_text_delta_event() -> None:
response = client._parse_chunk_from_openai(event, chat_options, function_call_ids) # type: ignore
assert len(response.contents) == 1
assert isinstance(response.contents[0], TextReasoningContent)
assert response.contents[0].type == "text_reasoning"
assert response.contents[0].text == "summary delta"
assert response.contents[0].raw_representation == event
mock_metadata.assert_called_once_with(event)
@@ -1447,7 +1879,7 @@ def test_streaming_reasoning_summary_text_done_event() -> None:
response = client._parse_chunk_from_openai(event, chat_options, function_call_ids) # type: ignore
assert len(response.contents) == 1
assert isinstance(response.contents[0], TextReasoningContent)
assert response.contents[0].type == "text_reasoning"
assert response.contents[0].text == "complete summary"
assert response.contents[0].raw_representation == event
mock_metadata.assert_called_once_with(event)
@@ -1488,8 +1920,8 @@ def test_streaming_reasoning_events_preserve_metadata() -> None:
assert reasoning_response.additional_properties == {"test": "metadata"}
# Content types should be different
assert isinstance(text_response.contents[0], TextContent)
assert isinstance(reasoning_response.contents[0], TextReasoningContent)
assert text_response.contents[0].type == "text"
assert reasoning_response.contents[0].type == "text_reasoning"
def test_parse_response_from_openai_image_generation_raw_base64():
@@ -1521,11 +1953,11 @@ def test_parse_response_from_openai_image_generation_raw_base64():
# Verify the response contains call + result with DataContent output
assert len(response.messages[0].contents) == 2
call_content, result_content = response.messages[0].contents
assert isinstance(call_content, ImageGenerationToolCallContent)
assert isinstance(result_content, ImageGenerationToolResultContent)
assert call_content.type == "image_generation_tool_call"
assert result_content.type == "image_generation_tool_result"
assert result_content.outputs
data_out = result_content.outputs
assert isinstance(data_out, DataContent)
assert data_out.type == "data"
assert data_out.uri.startswith("data:image/png;base64,")
assert data_out.media_type == "image/png"
@@ -1558,11 +1990,11 @@ def test_parse_response_from_openai_image_generation_existing_data_uri():
# Verify the response contains call + result with DataContent output
assert len(response.messages[0].contents) == 2
call_content, result_content = response.messages[0].contents
assert isinstance(call_content, ImageGenerationToolCallContent)
assert isinstance(result_content, ImageGenerationToolResultContent)
assert call_content.type == "image_generation_tool_call"
assert result_content.type == "image_generation_tool_result"
assert result_content.outputs
data_out = result_content.outputs
assert isinstance(data_out, DataContent)
assert data_out.type == "data"
assert data_out.uri == f"data:image/webp;base64,{valid_webp_base64}"
assert data_out.media_type == "image/webp"
@@ -1591,9 +2023,9 @@ def test_parse_response_from_openai_image_generation_format_detection():
with patch.object(client, "_get_metadata_from_response", return_value={}):
response_jpeg = client._parse_response_from_openai(mock_response_jpeg, options={}) # type: ignore
result_contents = response_jpeg.messages[0].contents
assert isinstance(result_contents[1], ImageGenerationToolResultContent)
assert result_contents[1].type == "image_generation_tool_result"
outputs = result_contents[1].outputs
assert outputs and isinstance(outputs, DataContent)
assert outputs and outputs.type == "data"
assert outputs.media_type == "image/jpeg"
assert "data:image/jpeg;base64," in outputs.uri
@@ -1617,7 +2049,7 @@ def test_parse_response_from_openai_image_generation_format_detection():
with patch.object(client, "_get_metadata_from_response", return_value={}):
response_webp = client._parse_response_from_openai(mock_response_webp, options={}) # type: ignore
outputs_webp = response_webp.messages[0].contents[1].outputs
assert outputs_webp and isinstance(outputs_webp, DataContent)
assert outputs_webp and outputs_webp.type == "data"
assert outputs_webp.media_type == "image/webp"
assert "data:image/webp;base64," in outputs_webp.uri
@@ -1650,7 +2082,7 @@ def test_parse_response_from_openai_image_generation_fallback():
# Verify it falls back to PNG format for unrecognized binary data
assert len(response.messages[0].contents) == 2
result_content = response.messages[0].contents[1]
assert isinstance(result_content, ImageGenerationToolResultContent)
assert result_content.type == "image_generation_tool_result"
assert result_content.outputs
content = result_content.outputs
assert content.media_type == "image/png"
@@ -1682,6 +2114,20 @@ async def test_prepare_options_store_parameter_handling() -> None:
assert "previous_response_id" not in options
async def test_conversation_id_precedence_kwargs_over_options() -> None:
"""When both kwargs and options contain conversation_id, kwargs wins."""
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
messages = [ChatMessage(role="user", text="Hello")]
# options has a stale response id, kwargs carries the freshest one
opts = {"conversation_id": "resp_old_123"}
run_opts = await client._prepare_options(messages, opts, conversation_id="resp_new_456") # type: ignore
# Verify kwargs takes precedence and maps to previous_response_id for resp_* IDs
assert run_opts.get("previous_response_id") == "resp_new_456"
assert "conversation" not in run_opts
def test_with_callable_api_key() -> None:
"""Test OpenAIResponsesClient initialization with callable API key."""
@@ -1944,7 +2390,7 @@ async def test_integration_streaming_file_search() -> None:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
if content.type == "text" and content.text:
full_message += content.text
await delete_vector_store(openai_responses_client, file_id, vector_store.vector_store_id)
@@ -5,7 +5,7 @@
import json
from datetime import datetime
from agent_framework._types import FunctionResultContent
from agent_framework import Content
from agent_framework.observability import _to_otel_part
@@ -14,7 +14,7 @@ def test_datetime_in_tool_results() -> None:
Reproduces issue #2219 where datetime objects caused TypeError.
"""
content = FunctionResultContent(
content = Content.from_function_result(
call_id="test-call",
result={"timestamp": datetime(2025, 11, 16, 10, 30, 0)},
)
@@ -11,9 +11,9 @@ from agent_framework import (
BaseAgent,
ChatMessage,
ChatMessageStore,
Content,
Role,
SequentialBuilder,
TextContent,
WorkflowOutputEvent,
WorkflowRunState,
WorkflowStatusEvent,
@@ -49,7 +49,7 @@ class _CountingAgent(BaseAgent):
**kwargs: Any,
) -> AsyncIterable[AgentResponseUpdate]:
self.call_count += 1
yield AgentResponseUpdate(contents=[TextContent(text=f"Response #{self.call_count}: {self.name}")])
yield AgentResponseUpdate(contents=[Content.from_text(text=f"Response #{self.call_count}: {self.name}")])
async def test_agent_executor_checkpoint_stores_and_restores_state() -> None:
@@ -19,12 +19,9 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
FunctionApprovalRequestContent,
FunctionCallContent,
FunctionResultContent,
Content,
RequestInfoEvent,
Role,
TextContent,
WorkflowBuilder,
WorkflowContext,
WorkflowOutputEvent,
@@ -60,14 +57,14 @@ class _ToolCallingAgent(BaseAgent):
"""Simulate streaming with tool calls and results."""
# First update: some text
yield AgentResponseUpdate(
contents=[TextContent(text="Let me search for that...")],
contents=[Content.from_text(text="Let me search for that...")],
role=Role.ASSISTANT,
)
# Second update: tool call (no text!)
yield AgentResponseUpdate(
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="call_123",
name="search",
arguments={"query": "weather"},
@@ -79,7 +76,7 @@ class _ToolCallingAgent(BaseAgent):
# Third update: tool result (no text!)
yield AgentResponseUpdate(
contents=[
FunctionResultContent(
Content.from_function_result(
call_id="call_123",
result={"temperature": 72, "condition": "sunny"},
)
@@ -89,7 +86,7 @@ class _ToolCallingAgent(BaseAgent):
# Fourth update: final text response
yield AgentResponseUpdate(
contents=[TextContent(text="The weather is sunny, 72°F.")],
contents=[Content.from_text(text="The weather is sunny, 72°F.")],
role=Role.ASSISTANT,
)
@@ -113,25 +110,25 @@ async def test_agent_executor_emits_tool_calls_in_streaming_mode() -> None:
# First event: text update
assert events[0].data is not None
assert isinstance(events[0].data.contents[0], TextContent)
assert events[0].data.contents[0].type == "text"
assert "Let me search" in events[0].data.contents[0].text
# Second event: function call
assert events[1].data is not None
assert isinstance(events[1].data.contents[0], FunctionCallContent)
assert events[1].data.contents[0].type == "function_call"
func_call = events[1].data.contents[0]
assert func_call.call_id == "call_123"
assert func_call.name == "search"
# Third event: function result
assert events[2].data is not None
assert isinstance(events[2].data.contents[0], FunctionResultContent)
assert events[2].data.contents[0].type == "function_result"
func_result = events[2].data.contents[0]
assert func_result.call_id == "call_123"
# Fourth event: final text
assert events[3].data is not None
assert isinstance(events[3].data.contents[0], TextContent)
assert events[3].data.contents[0].type == "text"
assert "sunny" in events[3].data.contents[0].text
@@ -161,10 +158,10 @@ class MockChatClient:
messages=ChatMessage(
role="assistant",
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="1", name="mock_tool_requiring_approval", arguments='{"query": "test"}'
),
FunctionCallContent(
Content.from_function_call(
call_id="2", name="mock_tool_requiring_approval", arguments='{"query": "test"}'
),
],
@@ -175,7 +172,7 @@ class MockChatClient:
messages=ChatMessage(
role="assistant",
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="1", name="mock_tool_requiring_approval", arguments='{"query": "test"}'
)
],
@@ -196,10 +193,10 @@ class MockChatClient:
if self._parallel_request:
yield ChatResponseUpdate(
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="1", name="mock_tool_requiring_approval", arguments='{"query": "test"}'
),
FunctionCallContent(
Content.from_function_call(
call_id="2", name="mock_tool_requiring_approval", arguments='{"query": "test"}'
),
],
@@ -208,15 +205,15 @@ class MockChatClient:
else:
yield ChatResponseUpdate(
contents=[
FunctionCallContent(
Content.from_function_call(
call_id="1", name="mock_tool_requiring_approval", arguments='{"query": "test"}'
)
],
role="assistant",
)
else:
yield ChatResponseUpdate(text=TextContent(text="Tool executed "), role="assistant")
yield ChatResponseUpdate(contents=[TextContent(text="successfully.")], role="assistant")
yield ChatResponseUpdate(text=Content.from_text(text="Tool executed "), role="assistant")
yield ChatResponseUpdate(contents=[Content.from_text(text="successfully.")], role="assistant")
self._iteration += 1
@@ -243,12 +240,14 @@ async def test_agent_executor_tool_call_with_approval() -> None:
# Assert
assert len(events.get_request_info_events()) == 1
approval_request = events.get_request_info_events()[0]
assert isinstance(approval_request.data, FunctionApprovalRequestContent)
assert approval_request.data.type == "function_approval_request"
assert approval_request.data.function_call.name == "mock_tool_requiring_approval"
assert approval_request.data.function_call.arguments == '{"query": "test"}'
# Act
events = await workflow.send_responses({approval_request.request_id: approval_request.data.create_response(True)})
events = await workflow.send_responses({
approval_request.request_id: approval_request.data.to_function_approval_response(True)
})
# Assert
final_response = events.get_outputs()
@@ -276,14 +275,14 @@ async def test_agent_executor_tool_call_with_approval_streaming() -> None:
# Assert
assert len(request_info_events) == 1
approval_request = request_info_events[0]
assert isinstance(approval_request.data, FunctionApprovalRequestContent)
assert approval_request.data.type == "function_approval_request"
assert approval_request.data.function_call.name == "mock_tool_requiring_approval"
assert approval_request.data.function_call.arguments == '{"query": "test"}'
# Act
output: str | None = None
async for event in workflow.send_responses_streaming({
approval_request.request_id: approval_request.data.create_response(True)
approval_request.request_id: approval_request.data.to_function_approval_response(True)
}):
if isinstance(event, WorkflowOutputEvent):
output = event.data
@@ -310,13 +309,13 @@ async def test_agent_executor_parallel_tool_call_with_approval() -> None:
# Assert
assert len(events.get_request_info_events()) == 2
for approval_request in events.get_request_info_events():
assert isinstance(approval_request.data, FunctionApprovalRequestContent)
assert approval_request.data.type == "function_approval_request"
assert approval_request.data.function_call.name == "mock_tool_requiring_approval"
assert approval_request.data.function_call.arguments == '{"query": "test"}'
# Act
responses = {
approval_request.request_id: approval_request.data.create_response(True) # type: ignore
approval_request.request_id: approval_request.data.to_function_approval_response(True) # type: ignore
for approval_request in events.get_request_info_events()
}
events = await workflow.send_responses(responses)
@@ -347,13 +346,13 @@ async def test_agent_executor_parallel_tool_call_with_approval_streaming() -> No
# Assert
assert len(request_info_events) == 2
for approval_request in request_info_events:
assert isinstance(approval_request.data, FunctionApprovalRequestContent)
assert approval_request.data.type == "function_approval_request"
assert approval_request.data.function_call.name == "mock_tool_requiring_approval"
assert approval_request.data.function_call.arguments == '{"query": "test"}'
# Act
responses = {
approval_request.request_id: approval_request.data.create_response(True) # type: ignore
approval_request.request_id: approval_request.data.to_function_approval_response(True) # type: ignore
for approval_request in request_info_events
}
@@ -3,7 +3,10 @@
from dataclasses import dataclass # noqa: I001
from typing import Any, cast
from agent_framework._workflows._checkpoint_encoding import (
DATACLASS_MARKER,
MODEL_MARKER,
decode_checkpoint_value,
encode_checkpoint_value,
)
@@ -126,3 +129,110 @@ def test_encode_decode_nested_structures() -> None:
assert response.data == "first response"
assert isinstance(response.original_request, SampleRequest)
assert response.original_request.request_id == "req-1"
def test_encode_allows_marker_key_without_value_key() -> None:
"""Test that encoding a dict with only the marker key (no 'value') is allowed."""
dict_with_marker_only = {
MODEL_MARKER: "some.module:FakeClass",
"other_key": "test",
}
encoded = encode_checkpoint_value(dict_with_marker_only)
assert MODEL_MARKER in encoded
assert "other_key" in encoded
def test_encode_allows_value_key_without_marker_key() -> None:
"""Test that encoding a dict with only 'value' key (no marker) is allowed."""
dict_with_value_only = {
"value": {"data": "test"},
"other_key": "test",
}
encoded = encode_checkpoint_value(dict_with_value_only)
assert "value" in encoded
assert "other_key" in encoded
def test_encode_allows_marker_with_value_key() -> None:
"""Test that encoding a dict with marker and 'value' keys is allowed.
This is allowed because legitimate encoded data may contain these keys,
and security is enforced at deserialization time by validating class types.
"""
dict_with_both = {
MODEL_MARKER: "some.module:SomeClass",
"value": {"data": "test"},
"strategy": "to_dict",
}
encoded = encode_checkpoint_value(dict_with_both)
assert MODEL_MARKER in encoded
assert "value" in encoded
class NotADataclass:
"""A regular class that is not a dataclass."""
def __init__(self, value: str) -> None:
self.value = value
def get_value(self) -> str:
return self.value
class NotAModel:
"""A regular class that does not support the model protocol."""
def __init__(self, value: str) -> None:
self.value = value
def get_value(self) -> str:
return self.value
def test_decode_rejects_non_dataclass_with_dataclass_marker() -> None:
"""Test that decode returns raw value when marked class is not a dataclass."""
# Manually construct a payload that claims NotADataclass is a dataclass
fake_payload = {
DATACLASS_MARKER: f"{NotADataclass.__module__}:{NotADataclass.__name__}",
"value": {"value": "test_value"},
}
decoded = decode_checkpoint_value(fake_payload)
# Should return the raw decoded value, not an instance of NotADataclass
assert isinstance(decoded, dict)
assert decoded["value"] == "test_value"
def test_decode_rejects_non_model_with_model_marker() -> None:
"""Test that decode returns raw value when marked class doesn't support model protocol."""
# Manually construct a payload that claims NotAModel supports the model protocol
fake_payload = {
MODEL_MARKER: f"{NotAModel.__module__}:{NotAModel.__name__}",
"strategy": "to_dict",
"value": {"value": "test_value"},
}
decoded = decode_checkpoint_value(fake_payload)
# Should return the raw decoded value, not an instance of NotAModel
assert isinstance(decoded, dict)
assert decoded["value"] == "test_value"
def test_encode_allows_nested_dict_with_marker_keys() -> None:
"""Test that encoding allows nested dicts containing marker patterns.
Security is enforced at deserialization time, not serialization time,
so legitimate encoded data can contain markers at any nesting level.
"""
nested_data = {
"outer": {
MODEL_MARKER: "some.module:SomeClass",
"value": {"data": "test"},
}
}
encoded = encode_checkpoint_value(nested_data)
assert "outer" in encoded
assert MODEL_MARKER in encoded["outer"]
@@ -0,0 +1,423 @@
# Copyright (c) Microsoft. All rights reserved.
from dataclasses import dataclass
from typing import Any
from agent_framework._workflows._checkpoint_encoding import (
_CYCLE_SENTINEL,
DATACLASS_MARKER,
MODEL_MARKER,
encode_checkpoint_value,
)
@dataclass
class SimpleDataclass:
"""A simple dataclass for testing encoding."""
name: str
value: int
@dataclass
class NestedDataclass:
"""A dataclass with nested dataclass field."""
outer_name: str
inner: SimpleDataclass
class ModelWithToDict:
"""A class that implements to_dict/from_dict protocol."""
def __init__(self, data: str) -> None:
self.data = data
def to_dict(self) -> dict[str, Any]:
return {"data": self.data}
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "ModelWithToDict":
return cls(data=d["data"])
class ModelWithToJson:
"""A class that implements to_json/from_json protocol."""
def __init__(self, data: str) -> None:
self.data = data
def to_json(self) -> str:
return f'{{"data": "{self.data}"}}'
@classmethod
def from_json(cls, json_str: str) -> "ModelWithToJson":
import json
d = json.loads(json_str)
return cls(data=d["data"])
class UnknownObject:
"""A class that doesn't support any serialization protocol."""
def __init__(self, value: str) -> None:
self.value = value
def __str__(self) -> str:
return f"UnknownObject({self.value})"
# --- Tests for primitive encoding ---
def test_encode_string() -> None:
"""Test encoding a string value."""
result = encode_checkpoint_value("hello")
assert result == "hello"
def test_encode_integer() -> None:
"""Test encoding an integer value."""
result = encode_checkpoint_value(42)
assert result == 42
def test_encode_float() -> None:
"""Test encoding a float value."""
result = encode_checkpoint_value(3.14)
assert result == 3.14
def test_encode_boolean_true() -> None:
"""Test encoding a True boolean value."""
result = encode_checkpoint_value(True)
assert result is True
def test_encode_boolean_false() -> None:
"""Test encoding a False boolean value."""
result = encode_checkpoint_value(False)
assert result is False
def test_encode_none() -> None:
"""Test encoding a None value."""
result = encode_checkpoint_value(None)
assert result is None
# --- Tests for collection encoding ---
def test_encode_empty_dict() -> None:
"""Test encoding an empty dictionary."""
result = encode_checkpoint_value({})
assert result == {}
def test_encode_simple_dict() -> None:
"""Test encoding a simple dictionary with primitive values."""
data = {"name": "test", "count": 5, "active": True}
result = encode_checkpoint_value(data)
assert result == {"name": "test", "count": 5, "active": True}
def test_encode_dict_with_non_string_keys() -> None:
"""Test encoding a dictionary with non-string keys (converted to strings)."""
data = {1: "one", 2: "two"}
result = encode_checkpoint_value(data)
assert result == {"1": "one", "2": "two"}
def test_encode_empty_list() -> None:
"""Test encoding an empty list."""
result = encode_checkpoint_value([])
assert result == []
def test_encode_simple_list() -> None:
"""Test encoding a simple list with primitive values."""
data = [1, 2, 3, "four"]
result = encode_checkpoint_value(data)
assert result == [1, 2, 3, "four"]
def test_encode_tuple() -> None:
"""Test encoding a tuple (converted to list)."""
data = (1, 2, 3)
result = encode_checkpoint_value(data)
assert result == [1, 2, 3]
def test_encode_set() -> None:
"""Test encoding a set (converted to list)."""
data = {1, 2, 3}
result = encode_checkpoint_value(data)
assert isinstance(result, list)
assert sorted(result) == [1, 2, 3]
def test_encode_nested_dict() -> None:
"""Test encoding a nested dictionary structure."""
data = {
"outer": {
"inner": {
"value": 42,
}
}
}
result = encode_checkpoint_value(data)
assert result == {"outer": {"inner": {"value": 42}}}
def test_encode_list_of_dicts() -> None:
"""Test encoding a list containing dictionaries."""
data = [{"a": 1}, {"b": 2}]
result = encode_checkpoint_value(data)
assert result == [{"a": 1}, {"b": 2}]
# --- Tests for dataclass encoding ---
def test_encode_simple_dataclass() -> None:
"""Test encoding a simple dataclass."""
obj = SimpleDataclass(name="test", value=42)
result = encode_checkpoint_value(obj)
assert isinstance(result, dict)
assert DATACLASS_MARKER in result
assert "value" in result
assert result["value"] == {"name": "test", "value": 42}
def test_encode_nested_dataclass() -> None:
"""Test encoding a dataclass with nested dataclass fields."""
inner = SimpleDataclass(name="inner", value=10)
outer = NestedDataclass(outer_name="outer", inner=inner)
result = encode_checkpoint_value(outer)
assert isinstance(result, dict)
assert DATACLASS_MARKER in result
assert "value" in result
outer_value = result["value"]
assert outer_value["outer_name"] == "outer"
assert DATACLASS_MARKER in outer_value["inner"]
def test_encode_list_of_dataclasses() -> None:
"""Test encoding a list containing dataclass instances."""
data = [
SimpleDataclass(name="first", value=1),
SimpleDataclass(name="second", value=2),
]
result = encode_checkpoint_value(data)
assert isinstance(result, list)
assert len(result) == 2
for item in result:
assert DATACLASS_MARKER in item
def test_encode_dict_with_dataclass_values() -> None:
"""Test encoding a dictionary with dataclass values."""
data = {
"item1": SimpleDataclass(name="first", value=1),
"item2": SimpleDataclass(name="second", value=2),
}
result = encode_checkpoint_value(data)
assert isinstance(result, dict)
assert DATACLASS_MARKER in result["item1"]
assert DATACLASS_MARKER in result["item2"]
# --- Tests for model protocol encoding ---
def test_encode_model_with_to_dict() -> None:
"""Test encoding an object implementing to_dict/from_dict protocol."""
obj = ModelWithToDict(data="test_data")
result = encode_checkpoint_value(obj)
assert isinstance(result, dict)
assert MODEL_MARKER in result
assert result["strategy"] == "to_dict"
assert result["value"] == {"data": "test_data"}
def test_encode_model_with_to_json() -> None:
"""Test encoding an object implementing to_json/from_json protocol."""
obj = ModelWithToJson(data="test_data")
result = encode_checkpoint_value(obj)
assert isinstance(result, dict)
assert MODEL_MARKER in result
assert result["strategy"] == "to_json"
assert '"data": "test_data"' in result["value"]
# --- Tests for unknown object encoding ---
def test_encode_unknown_object_fallback_to_string() -> None:
"""Test that unknown objects are encoded as strings."""
obj = UnknownObject(value="test")
result = encode_checkpoint_value(obj)
assert isinstance(result, str)
assert "UnknownObject" in result
# --- Tests for cycle detection ---
def test_encode_dict_with_self_reference() -> None:
"""Test that dict self-references are detected and handled."""
data: dict[str, Any] = {"name": "test"}
data["self"] = data # Create circular reference
result = encode_checkpoint_value(data)
assert result["name"] == "test"
assert result["self"] == _CYCLE_SENTINEL
def test_encode_list_with_self_reference() -> None:
"""Test that list self-references are detected and handled."""
data: list[Any] = [1, 2]
data.append(data) # Create circular reference
result = encode_checkpoint_value(data)
assert result[0] == 1
assert result[1] == 2
assert result[2] == _CYCLE_SENTINEL
# --- Tests for reserved keyword handling ---
# Note: Security is enforced at deserialization time by validating class types,
# not at serialization time. This allows legitimate encoded data to be re-encoded.
def test_encode_allows_dict_with_model_marker_and_value() -> None:
"""Test that encoding a dict with MODEL_MARKER and 'value' is allowed.
Security is enforced at deserialization time, not serialization time.
"""
data = {
MODEL_MARKER: "some.module:SomeClass",
"value": {"data": "test"},
}
result = encode_checkpoint_value(data)
assert MODEL_MARKER in result
assert "value" in result
def test_encode_allows_dict_with_dataclass_marker_and_value() -> None:
"""Test that encoding a dict with DATACLASS_MARKER and 'value' is allowed.
Security is enforced at deserialization time, not serialization time.
"""
data = {
DATACLASS_MARKER: "some.module:SomeClass",
"value": {"field": "test"},
}
result = encode_checkpoint_value(data)
assert DATACLASS_MARKER in result
assert "value" in result
def test_encode_allows_nested_dict_with_marker_keys() -> None:
"""Test that encoding nested dict with marker keys is allowed.
Security is enforced at deserialization time, not serialization time.
"""
nested_data = {
"outer": {
MODEL_MARKER: "some.module:SomeClass",
"value": {"data": "test"},
}
}
result = encode_checkpoint_value(nested_data)
assert "outer" in result
assert MODEL_MARKER in result["outer"]
def test_encode_allows_marker_without_value() -> None:
"""Test that a dict with marker key but without 'value' key is allowed."""
data = {
MODEL_MARKER: "some.module:SomeClass",
"other_key": "allowed",
}
result = encode_checkpoint_value(data)
assert MODEL_MARKER in result
assert result["other_key"] == "allowed"
def test_encode_allows_value_without_marker() -> None:
"""Test that a dict with 'value' key but without marker is allowed."""
data = {
"value": {"nested": "data"},
"other_key": "allowed",
}
result = encode_checkpoint_value(data)
assert "value" in result
assert result["other_key"] == "allowed"
# --- Tests for max depth protection ---
def test_encode_deep_nesting_triggers_max_depth() -> None:
"""Test that very deep nesting triggers max depth protection."""
# Create a deeply nested structure (over 100 levels)
data: dict[str, Any] = {"level": 0}
current = data
for i in range(105):
current["nested"] = {"level": i + 1}
current = current["nested"]
result = encode_checkpoint_value(data)
# Navigate to find the max_depth sentinel
current_result = result
found_max_depth = False
for _ in range(110):
if isinstance(current_result, dict) and "nested" in current_result:
current_result = current_result["nested"]
if current_result == "<max_depth>":
found_max_depth = True
break
else:
break
assert found_max_depth, "Expected <max_depth> sentinel to be found in deeply nested structure"
# --- Tests for mixed complex structures ---
def test_encode_complex_mixed_structure() -> None:
"""Test encoding a complex structure with mixed types."""
data = {
"string_value": "hello",
"int_value": 42,
"float_value": 3.14,
"bool_value": True,
"none_value": None,
"list_value": [1, 2, 3],
"nested_dict": {"a": 1, "b": 2},
"dataclass_value": SimpleDataclass(name="test", value=100),
}
result = encode_checkpoint_value(data)
assert result["string_value"] == "hello"
assert result["int_value"] == 42
assert result["float_value"] == 3.14
assert result["bool_value"] is True
assert result["none_value"] is None
assert result["list_value"] == [1, 2, 3]
assert result["nested_dict"] == {"a": 1, "b": 2}
assert DATACLASS_MARKER in result["dataclass_value"]
@@ -14,10 +14,10 @@ from agent_framework import (
AgentThread,
BaseAgent,
ChatMessage,
Content,
Executor,
Role,
SequentialBuilder,
TextContent,
WorkflowBuilder,
WorkflowContext,
WorkflowRunState,
@@ -50,7 +50,7 @@ class _SimpleAgent(BaseAgent):
**kwargs: Any,
) -> AsyncIterable[AgentResponseUpdate]:
# This agent does not support streaming; yield a single complete response
yield AgentResponseUpdate(contents=[TextContent(text=self._reply_text)])
yield AgentResponseUpdate(contents=[Content.from_text(text=self._reply_text)])
class _CaptureFullConversation(Executor):
@@ -136,7 +136,7 @@ class _CaptureAgent(BaseAgent):
elif isinstance(m, str):
norm.append(ChatMessage(role=Role.USER, text=m))
self._last_messages = norm
yield AgentResponseUpdate(contents=[TextContent(text=self._reply_text)])
yield AgentResponseUpdate(contents=[Content.from_text(text=self._reply_text)])
async def test_sequential_adapter_uses_full_conversation() -> None:
@@ -17,6 +17,7 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
Content,
GroupChatBuilder,
GroupChatState,
MagenticContext,
@@ -25,7 +26,6 @@ from agent_framework import (
MagenticProgressLedgerItem,
RequestInfoEvent,
Role,
TextContent,
WorkflowOutputEvent,
WorkflowRunState,
WorkflowStatusEvent,
@@ -57,7 +57,7 @@ class StubAgent(BaseAgent):
) -> AsyncIterable[AgentResponseUpdate]:
async def _stream() -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(
contents=[TextContent(text=self._reply_text)], role=Role.ASSISTANT, author_name=self.name
contents=[Content.from_text(text=self._reply_text)], role=Role.ASSISTANT, author_name=self.name
)
return _stream()
@@ -141,7 +141,7 @@ class StubManagerAgent(ChatAgent):
async def _stream_initial() -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(
contents=[
TextContent(
Content.from_text(
text=(
'{"terminate": false, "reason": "Selecting agent", '
'"next_speaker": "agent", "final_message": null}'
@@ -157,7 +157,7 @@ class StubManagerAgent(ChatAgent):
async def _stream_final() -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(
contents=[
TextContent(
Content.from_text(
text=(
'{"terminate": true, "reason": "Task complete", '
'"next_speaker": null, "final_message": "agent manager final"}'
@@ -11,12 +11,11 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
FunctionCallContent,
Content,
HandoffAgentUserRequest,
HandoffBuilder,
RequestInfoEvent,
Role,
TextContent,
WorkflowEvent,
WorkflowOutputEvent,
resolve_agent_id,
@@ -74,14 +73,16 @@ def _build_reply_contents(
agent_name: str,
handoff_to: str | None,
call_id: str | None,
) -> list[TextContent | FunctionCallContent]:
contents: list[TextContent | FunctionCallContent] = []
) -> list[Content]:
contents: list[Content] = []
if handoff_to and call_id:
contents.append(
FunctionCallContent(call_id=call_id, name=f"handoff_to_{handoff_to}", arguments={"handoff_to": handoff_to})
Content.from_function_call(
call_id=call_id, name=f"handoff_to_{handoff_to}", arguments={"handoff_to": handoff_to}
)
)
text = f"{agent_name} reply"
contents.append(TextContent(text=text))
contents.append(Content.from_text(text=text))
return contents
@@ -15,6 +15,7 @@ from agent_framework import (
AgentThread,
BaseAgent,
ChatMessage,
Content,
Executor,
GroupChatRequestMessage,
MagenticBuilder,
@@ -28,7 +29,6 @@ from agent_framework import (
RequestInfoEvent,
Role,
StandardMagenticManager,
TextContent,
Workflow,
WorkflowCheckpoint,
WorkflowCheckpointException,
@@ -172,7 +172,7 @@ class StubAgent(BaseAgent):
) -> AsyncIterable[AgentResponseUpdate]:
async def _stream() -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(
contents=[TextContent(text=self._reply_text)], role=Role.ASSISTANT, author_name=self.name
contents=[Content.from_text(text=self._reply_text)], role=Role.ASSISTANT, author_name=self.name
)
return _stream()
@@ -541,7 +541,7 @@ class StubThreadAgent(BaseAgent):
async def run_stream(self, messages=None, *, thread=None, **kwargs): # type: ignore[override]
yield AgentResponseUpdate(
contents=[TextContent(text="thread-ok")],
contents=[Content.from_text(text="thread-ok")],
author_name=self.name,
role=Role.ASSISTANT,
)
@@ -563,7 +563,7 @@ class StubAssistantsAgent(BaseAgent):
async def run_stream(self, messages=None, *, thread=None, **kwargs): # type: ignore[override]
yield AgentResponseUpdate(
contents=[TextContent(text="assistants-ok")],
contents=[Content.from_text(text="assistants-ok")],
author_name=self.name,
role=Role.ASSISTANT,
)
@@ -12,10 +12,10 @@ from agent_framework import (
AgentThread,
BaseAgent,
ChatMessage,
Content,
Executor,
Role,
SequentialBuilder,
TextContent,
TypeCompatibilityError,
WorkflowContext,
WorkflowOutputEvent,
@@ -46,7 +46,7 @@ class _EchoAgent(BaseAgent):
**kwargs: Any,
) -> AsyncIterable[AgentResponseUpdate]:
# Minimal async generator with one assistant update
yield AgentResponseUpdate(contents=[TextContent(text=f"{self.name} reply")])
yield AgentResponseUpdate(contents=[Content.from_text(text=f"{self.name} reply")])
class _SummarizerExec(Executor):
@@ -18,12 +18,12 @@ from agent_framework import (
AgentThread,
BaseAgent,
ChatMessage,
Content,
Executor,
FileCheckpointStorage,
Message,
RequestInfoEvent,
Role,
TextContent,
WorkflowBuilder,
WorkflowCheckpointException,
WorkflowContext,
@@ -881,7 +881,7 @@ class _StreamingTestAgent(BaseAgent):
"""Streaming run - yields incremental updates."""
# Simulate streaming by yielding character by character
for char in self._reply_text:
yield AgentResponseUpdate(contents=[TextContent(text=char)])
yield AgentResponseUpdate(contents=[Content.from_text(text=char)])
async def test_agent_streaming_vs_non_streaming() -> None:
@@ -14,16 +14,9 @@ from agent_framework import (
AgentThread,
ChatMessage,
ChatMessageStore,
DataContent,
Content,
Executor,
FunctionApprovalRequestContent,
FunctionApprovalResponseContent,
FunctionCallContent,
FunctionResultContent,
Role,
TextContent,
UriContent,
UsageContent,
UsageDetails,
WorkflowAgent,
WorkflowBuilder,
@@ -44,17 +37,15 @@ class SimpleExecutor(Executor):
@handler
async def handle_message(self, message: list[ChatMessage], ctx: WorkflowContext[list[ChatMessage]]) -> None:
input_text = (
message[0].contents[0].text if message and isinstance(message[0].contents[0], TextContent) else "no input"
)
input_text = message[0].contents[0].text if message and message[0].contents[0].type == "text" else "no input"
response_text = f"{self.response_text}: {input_text}"
# Create response message for both streaming and non-streaming cases
response_message = ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text=response_text)])
response_message = ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text(text=response_text)])
# Emit update event.
streaming_update = AgentResponseUpdate(
contents=[TextContent(text=response_text)], role=Role.ASSISTANT, message_id=str(uuid.uuid4())
contents=[Content.from_text(text=response_text)], role=Role.ASSISTANT, message_id=str(uuid.uuid4())
)
await ctx.add_event(AgentRunUpdateEvent(executor_id=self.id, data=streaming_update))
@@ -76,7 +67,7 @@ class RequestingExecutor(Executor):
) -> None:
# Handle the response and emit completion response
update = AgentResponseUpdate(
contents=[TextContent(text="Request completed successfully")],
contents=[Content.from_text(text="Request completed successfully")],
role=Role.ASSISTANT,
message_id=str(uuid.uuid4()),
)
@@ -99,10 +90,10 @@ class ConversationHistoryCapturingExecutor(Executor):
message_count = len(messages)
response_text = f"Received {message_count} messages"
response_message = ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text=response_text)])
response_message = ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text(text=response_text)])
streaming_update = AgentResponseUpdate(
contents=[TextContent(text=response_text)], role=Role.ASSISTANT, message_id=str(uuid.uuid4())
contents=[Content.from_text(text=response_text)], role=Role.ASSISTANT, message_id=str(uuid.uuid4())
)
await ctx.add_event(AgentRunUpdateEvent(executor_id=self.id, data=streaming_update))
await ctx.send_message([response_message])
@@ -134,7 +125,7 @@ class TestWorkflowAgent:
for message in result.messages:
first_content = message.contents[0]
if isinstance(first_content, TextContent):
if first_content.type == "text":
text = first_content.text
if text.startswith("Step1:"):
step1_messages.append(message)
@@ -172,11 +163,11 @@ class TestWorkflowAgent:
# Verify we got a streaming update
assert updates[0].contents is not None
first_content: TextContent = updates[0].contents[0] # type: ignore[assignment]
second_content: TextContent = updates[1].contents[0] # type: ignore[assignment]
assert isinstance(first_content, TextContent)
first_content: Content = updates[0].contents[0] # type: ignore[assignment]
second_content: Content = updates[1].contents[0] # type: ignore[assignment]
assert first_content.type == "text"
assert "Streaming1: Test input" in first_content.text
assert isinstance(second_content, TextContent)
assert second_content.type == "text"
assert "Streaming2: Streaming1: Test input" in second_content.text
async def test_end_to_end_request_info_handling(self):
@@ -200,17 +191,15 @@ class TestWorkflowAgent:
approval_update: AgentResponseUpdate | None = None
for update in updates:
if any(isinstance(content, FunctionApprovalRequestContent) for content in update.contents):
if any(content.type == "function_approval_request" for content in update.contents):
approval_update = update
break
assert approval_update is not None, "Should have received a request_info approval request"
function_call = next(
content for content in approval_update.contents if isinstance(content, FunctionCallContent)
)
function_call = next(content for content in approval_update.contents if content.type == "function_call")
approval_request = next(
content for content in approval_update.contents if isinstance(content, FunctionApprovalRequestContent)
content for content in approval_update.contents if content.type == "function_approval_request"
)
# Verify the function call has expected structure
@@ -233,10 +222,10 @@ class TestWorkflowAgent:
data="User provided answer",
).to_dict()
approval_response = FunctionApprovalResponseContent(
approval_response = Content.from_function_approval_response(
approved=True,
id=approval_request.id,
function_call=FunctionCallContent(
function_call=Content.from_function_call(
call_id=function_call.call_id,
name=function_call.name,
arguments=response_args,
@@ -306,7 +295,7 @@ class TestWorkflowAgent:
workflow = WorkflowBuilder().set_start_executor(yielding_executor).build()
# Run directly - should return WorkflowOutputEvent in result
direct_result = await workflow.run([ChatMessage(role=Role.USER, contents=[TextContent(text="hello")])])
direct_result = await workflow.run([ChatMessage(role=Role.USER, contents=[Content.from_text(text="hello")])])
direct_outputs = direct_result.get_outputs()
assert len(direct_outputs) == 1
assert direct_outputs[0] == "processed: hello"
@@ -340,14 +329,14 @@ class TestWorkflowAgent:
assert "second output" in texts
async def test_workflow_as_agent_yield_output_with_content_types(self) -> None:
"""Test that yield_output preserves different content types (TextContent, DataContent, etc.)."""
"""Test that yield_output preserves different content types (Content, Content, etc.)."""
@executor
async def content_yielding_executor(messages: list[ChatMessage], ctx: WorkflowContext) -> None:
# Yield different content types
await ctx.yield_output(TextContent(text="text content"))
await ctx.yield_output(DataContent(data=b"binary data", media_type="application/octet-stream"))
await ctx.yield_output(UriContent(uri="https://example.com/image.png", media_type="image/png"))
await ctx.yield_output(Content.from_text(text="text content"))
await ctx.yield_output(Content.from_data(data=b"binary data", media_type="application/octet-stream"))
await ctx.yield_output(Content.from_uri(uri="https://example.com/image.png", media_type="image/png"))
workflow = WorkflowBuilder().set_start_executor(content_yielding_executor).build()
agent = workflow.as_agent("content-test-agent")
@@ -358,13 +347,13 @@ class TestWorkflowAgent:
assert len(result.messages) == 3
# Verify each content type is preserved
assert isinstance(result.messages[0].contents[0], TextContent)
assert result.messages[0].contents[0].type == "text"
assert result.messages[0].contents[0].text == "text content"
assert isinstance(result.messages[1].contents[0], DataContent)
assert result.messages[1].contents[0].type == "data"
assert result.messages[1].contents[0].media_type == "application/octet-stream"
assert isinstance(result.messages[2].contents[0], UriContent)
assert result.messages[2].contents[0].type == "uri"
assert result.messages[2].contents[0].uri == "https://example.com/image.png"
async def test_workflow_as_agent_yield_output_with_chat_message(self) -> None:
@@ -374,7 +363,7 @@ class TestWorkflowAgent:
async def chat_message_executor(messages: list[ChatMessage], ctx: WorkflowContext) -> None:
msg = ChatMessage(
role=Role.ASSISTANT,
contents=[TextContent(text="response text")],
contents=[Content.from_text(text="response text")],
author_name="custom-author",
)
await ctx.yield_output(msg)
@@ -404,7 +393,7 @@ class TestWorkflowAgent:
async def raw_yielding_executor(messages: list[ChatMessage], ctx: WorkflowContext) -> None:
# Yield different types of data
await ctx.yield_output("simple string")
await ctx.yield_output(TextContent(text="text content"))
await ctx.yield_output(Content.from_text(text="text content"))
custom = CustomData(42)
await ctx.yield_output(custom)
@@ -420,7 +409,7 @@ class TestWorkflowAgent:
# Verify raw_representation is set for each update
assert updates[0].raw_representation == "simple string"
assert isinstance(updates[1].raw_representation, TextContent)
assert updates[1].raw_representation.type == "text"
assert updates[1].raw_representation.text == "text content"
assert isinstance(updates[2].raw_representation, CustomData)
assert updates[2].raw_representation.value == 42
@@ -428,19 +417,19 @@ class TestWorkflowAgent:
async def test_workflow_as_agent_yield_output_with_list_of_chat_messages(self) -> None:
"""Test that yield_output with list[ChatMessage] extracts contents from all messages.
Note: TextContent items are coalesced by _finalize_response, so multiple text contents
become a single merged TextContent in the final response.
Note: Content items are coalesced by _finalize_response, so multiple text contents
become a single merged Content in the final response.
"""
@executor
async def list_yielding_executor(messages: list[ChatMessage], ctx: WorkflowContext) -> None:
# Yield a list of ChatMessages (as SequentialBuilder does)
msg_list = [
ChatMessage(role=Role.USER, contents=[TextContent(text="first message")]),
ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text="second message")]),
ChatMessage(role=Role.USER, contents=[Content.from_text(text="first message")]),
ChatMessage(role=Role.ASSISTANT, contents=[Content.from_text(text="second message")]),
ChatMessage(
role=Role.ASSISTANT,
contents=[TextContent(text="third"), TextContent(text="fourth")],
contents=[Content.from_text(text="third"), Content.from_text(text="fourth")],
),
]
await ctx.yield_output(msg_list)
@@ -455,7 +444,7 @@ class TestWorkflowAgent:
assert len(updates) == 1
assert len(updates[0].contents) == 4
texts = [c.text for c in updates[0].contents if isinstance(c, TextContent)]
texts = [c.text for c in updates[0].contents if c.type == "text"]
assert texts == ["first message", "second message", "third", "fourth"]
# Verify run() coalesces text contents (expected behavior)
@@ -463,7 +452,7 @@ class TestWorkflowAgent:
assert isinstance(result, AgentResponse)
assert len(result.messages) == 1
# TextContent items are coalesced into one
# Content items are coalesced into one
assert len(result.messages[0].contents) == 1
assert result.messages[0].text == "first messagesecond messagethirdfourth"
@@ -599,7 +588,7 @@ class TestWorkflowAgent:
) -> AsyncIterable[AgentResponseUpdate]:
for word in self._response_text.split():
yield AgentResponseUpdate(
contents=[TextContent(text=word + " ")],
contents=[Content.from_text(text=word + " ")],
role=Role.ASSISTANT,
author_name=self._name,
)
@@ -672,7 +661,7 @@ class TestWorkflowAgent:
self, messages: Any, *, thread: AgentThread | None = None, **kwargs: Any
) -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(
contents=[TextContent(text=self._response_text)],
contents=[Content.from_text(text=self._response_text)],
role=Role.ASSISTANT,
author_name=self._name,
)
@@ -738,7 +727,7 @@ class TestWorkflowAgentAuthorName:
async def handle_message(self, message: list[ChatMessage], ctx: WorkflowContext[list[ChatMessage]]) -> None:
# Emit update with explicit author_name
update = AgentResponseUpdate(
contents=[TextContent(text="Response with author")],
contents=[Content.from_text(text="Response with author")],
role=Role.ASSISTANT,
author_name="custom_author_name", # Explicitly set
message_id=str(uuid.uuid4()),
@@ -790,7 +779,7 @@ class TestWorkflowAgentMergeUpdates:
updates = [
# Response B, Message 2 (latest in resp B)
AgentResponseUpdate(
contents=[TextContent(text="RespB-Msg2")],
contents=[Content.from_text(text="RespB-Msg2")],
role=Role.ASSISTANT,
response_id="resp-b",
message_id="msg-2",
@@ -798,7 +787,7 @@ class TestWorkflowAgentMergeUpdates:
),
# Response A, Message 1 (earliest overall)
AgentResponseUpdate(
contents=[TextContent(text="RespA-Msg1")],
contents=[Content.from_text(text="RespA-Msg1")],
role=Role.ASSISTANT,
response_id="resp-a",
message_id="msg-1",
@@ -806,7 +795,7 @@ class TestWorkflowAgentMergeUpdates:
),
# Response B, Message 1 (earlier in resp B)
AgentResponseUpdate(
contents=[TextContent(text="RespB-Msg1")],
contents=[Content.from_text(text="RespB-Msg1")],
role=Role.ASSISTANT,
response_id="resp-b",
message_id="msg-1",
@@ -814,7 +803,7 @@ class TestWorkflowAgentMergeUpdates:
),
# Response A, Message 2 (later in resp A)
AgentResponseUpdate(
contents=[TextContent(text="RespA-Msg2")],
contents=[Content.from_text(text="RespA-Msg2")],
role=Role.ASSISTANT,
response_id="resp-a",
message_id="msg-2",
@@ -822,7 +811,7 @@ class TestWorkflowAgentMergeUpdates:
),
# Global dangling update (no response_id) - should go at end
AgentResponseUpdate(
contents=[TextContent(text="Global-Dangling")],
contents=[Content.from_text(text="Global-Dangling")],
role=Role.ASSISTANT,
response_id=None,
message_id="msg-global",
@@ -841,9 +830,7 @@ class TestWorkflowAgentMergeUpdates:
# Verify ordering: responses are processed by response_id groups,
# within each group messages are chronologically ordered,
# global dangling goes at the end
message_texts = [
msg.contents[0].text if isinstance(msg.contents[0], TextContent) else "" for msg in result.messages
]
message_texts = [msg.contents[0].text if msg.contents[0].type == "text" else "" for msg in result.messages]
# The exact order depends on dict iteration order for response_ids,
# but within each response group, chronological order should be maintained
@@ -894,9 +881,9 @@ class TestWorkflowAgentMergeUpdates:
updates = [
AgentResponseUpdate(
contents=[
TextContent(text="First"),
UsageContent(
details=UsageDetails(input_token_count=10, output_token_count=5, total_token_count=15)
Content.from_text(text="First"),
Content.from_usage(
usage_details={"input_token_count": 10, "output_token_count": 5, "total_token_count": 15}
),
],
role=Role.ASSISTANT,
@@ -907,9 +894,9 @@ class TestWorkflowAgentMergeUpdates:
),
AgentResponseUpdate(
contents=[
TextContent(text="Second"),
UsageContent(
details=UsageDetails(input_token_count=20, output_token_count=8, total_token_count=28)
Content.from_text(text="Second"),
Content.from_usage(
usage_details={"input_token_count": 20, "output_token_count": 8, "total_token_count": 28}
),
],
role=Role.ASSISTANT,
@@ -920,8 +907,10 @@ class TestWorkflowAgentMergeUpdates:
),
AgentResponseUpdate(
contents=[
TextContent(text="Third"),
UsageContent(details=UsageDetails(input_token_count=5, output_token_count=3, total_token_count=8)),
Content.from_text(text="Third"),
Content.from_usage(
usage_details={"input_token_count": 5, "output_token_count": 3, "total_token_count": 8}
),
],
role=Role.ASSISTANT,
response_id="resp-1", # Same response_id as first
@@ -985,7 +974,7 @@ class TestWorkflowAgentMergeUpdates:
updates = [
# User question
AgentResponseUpdate(
contents=[TextContent(text="What is the weather?")],
contents=[Content.from_text(text="What is the weather?")],
role=Role.USER,
response_id="resp-1",
message_id="msg-1",
@@ -993,7 +982,9 @@ class TestWorkflowAgentMergeUpdates:
),
# Assistant with function call
AgentResponseUpdate(
contents=[FunctionCallContent(call_id=call_id, name="get_weather", arguments='{"location": "NYC"}')],
contents=[
Content.from_function_call(call_id=call_id, name="get_weather", arguments='{"location": "NYC"}')
],
role=Role.ASSISTANT,
response_id="resp-1",
message_id="msg-2",
@@ -1002,7 +993,7 @@ class TestWorkflowAgentMergeUpdates:
# Function result: no response_id previously caused this to go to global_dangling
# and be placed at the end (the bug); fix now correctly associates via call_id
AgentResponseUpdate(
contents=[FunctionResultContent(call_id=call_id, result="Sunny, 72F")],
contents=[Content.from_function_result(call_id=call_id, result="Sunny, 72F")],
role=Role.TOOL,
response_id=None,
message_id="msg-3",
@@ -1010,7 +1001,7 @@ class TestWorkflowAgentMergeUpdates:
),
# Final assistant answer
AgentResponseUpdate(
contents=[TextContent(text="The weather in NYC is sunny and 72F.")],
contents=[Content.from_text(text="The weather in NYC is sunny and 72F.")],
role=Role.ASSISTANT,
response_id="resp-1",
message_id="msg-4",
@@ -1026,11 +1017,11 @@ class TestWorkflowAgentMergeUpdates:
content_sequence = []
for msg in result.messages:
for content in msg.contents:
if isinstance(content, TextContent):
if content.type == "text":
content_sequence.append(("text", msg.role))
elif isinstance(content, FunctionCallContent):
elif content.type == "function_call":
content_sequence.append(("function_call", msg.role))
elif isinstance(content, FunctionResultContent):
elif content.type == "function_result":
content_sequence.append(("function_result", msg.role))
# Verify correct ordering: user -> function_call -> function_result -> assistant_answer
@@ -1051,10 +1042,10 @@ class TestWorkflowAgentMergeUpdates:
function_result_idx = None
for i, msg in enumerate(result.messages):
for content in msg.contents:
if isinstance(content, FunctionCallContent):
if content.type == "function_call":
function_call_idx = i
assert content.call_id == call_id
elif isinstance(content, FunctionResultContent):
elif content.type == "function_result":
function_result_idx = i
assert content.call_id == call_id
@@ -1081,7 +1072,7 @@ class TestWorkflowAgentMergeUpdates:
updates = [
# User question
AgentResponseUpdate(
contents=[TextContent(text="What's the weather and time?")],
contents=[Content.from_text(text="What's the weather and time?")],
role=Role.USER,
response_id="resp-1",
message_id="msg-1",
@@ -1089,7 +1080,9 @@ class TestWorkflowAgentMergeUpdates:
),
# Assistant with first function call
AgentResponseUpdate(
contents=[FunctionCallContent(call_id=call_id_1, name="get_weather", arguments='{"location": "NYC"}')],
contents=[
Content.from_function_call(call_id=call_id_1, name="get_weather", arguments='{"location": "NYC"}')
],
role=Role.ASSISTANT,
response_id="resp-1",
message_id="msg-2",
@@ -1097,7 +1090,9 @@ class TestWorkflowAgentMergeUpdates:
),
# Assistant with second function call
AgentResponseUpdate(
contents=[FunctionCallContent(call_id=call_id_2, name="get_time", arguments='{"timezone": "EST"}')],
contents=[
Content.from_function_call(call_id=call_id_2, name="get_time", arguments='{"timezone": "EST"}')
],
role=Role.ASSISTANT,
response_id="resp-1",
message_id="msg-3",
@@ -1105,7 +1100,7 @@ class TestWorkflowAgentMergeUpdates:
),
# Second function result arrives first (no response_id)
AgentResponseUpdate(
contents=[FunctionResultContent(call_id=call_id_2, result="3:00 PM EST")],
contents=[Content.from_function_result(call_id=call_id_2, result="3:00 PM EST")],
role=Role.TOOL,
response_id=None,
message_id="msg-4",
@@ -1113,7 +1108,7 @@ class TestWorkflowAgentMergeUpdates:
),
# First function result arrives second (no response_id)
AgentResponseUpdate(
contents=[FunctionResultContent(call_id=call_id_1, result="Sunny, 72F")],
contents=[Content.from_function_result(call_id=call_id_1, result="Sunny, 72F")],
role=Role.TOOL,
response_id=None,
message_id="msg-5",
@@ -1121,7 +1116,7 @@ class TestWorkflowAgentMergeUpdates:
),
# Final assistant answer
AgentResponseUpdate(
contents=[TextContent(text="It's sunny (72F) and 3 PM in NYC.")],
contents=[Content.from_text(text="It's sunny (72F) and 3 PM in NYC.")],
role=Role.ASSISTANT,
response_id="resp-1",
message_id="msg-6",
@@ -1137,11 +1132,11 @@ class TestWorkflowAgentMergeUpdates:
content_sequence = []
for msg in result.messages:
for content in msg.contents:
if isinstance(content, TextContent):
if content.type == "text":
content_sequence.append(("text", None))
elif isinstance(content, FunctionCallContent):
elif content.type == "function_call":
content_sequence.append(("function_call", content.call_id))
elif isinstance(content, FunctionResultContent):
elif content.type == "function_result":
content_sequence.append(("function_result", content.call_id))
# Verify all function results appear before the final assistant text
@@ -1172,7 +1167,7 @@ class TestWorkflowAgentMergeUpdates:
"""
updates = [
AgentResponseUpdate(
contents=[TextContent(text="Hello")],
contents=[Content.from_text(text="Hello")],
role=Role.USER,
response_id="resp-1",
message_id="msg-1",
@@ -1180,14 +1175,14 @@ class TestWorkflowAgentMergeUpdates:
),
# Function result with no matching call
AgentResponseUpdate(
contents=[FunctionResultContent(call_id="orphan_call_id", result="orphan result")],
contents=[Content.from_function_result(call_id="orphan_call_id", result="orphan result")],
role=Role.TOOL,
response_id=None,
message_id="msg-2",
created_at="2024-01-01T12:00:01Z",
),
AgentResponseUpdate(
contents=[TextContent(text="Goodbye")],
contents=[Content.from_text(text="Goodbye")],
role=Role.ASSISTANT,
response_id="resp-1",
message_id="msg-3",
@@ -1203,9 +1198,9 @@ class TestWorkflowAgentMergeUpdates:
content_types = []
for msg in result.messages:
for content in msg.contents:
if isinstance(content, TextContent):
if content.type == "text":
content_types.append("text")
elif isinstance(content, FunctionResultContent):
elif content.type == "function_result":
content_types.append("function_result")
# Order: text (user), text (assistant), function_result (orphan at end)
@@ -12,12 +12,12 @@ from agent_framework import (
BaseAgent,
ChatMessage,
ConcurrentBuilder,
Content,
GroupChatBuilder,
GroupChatState,
HandoffBuilder,
Role,
SequentialBuilder,
TextContent,
WorkflowRunState,
WorkflowStatusEvent,
ai_function,
@@ -67,7 +67,7 @@ class _KwargsCapturingAgent(BaseAgent):
**kwargs: Any,
) -> AsyncIterable[AgentResponseUpdate]:
self.captured_kwargs.append(dict(kwargs))
yield AgentResponseUpdate(contents=[TextContent(text=f"{self.name} response")])
yield AgentResponseUpdate(contents=[Content.from_text(text=f"{self.name} response")])
# region Sequential Builder Tests