Skip to content

Python: [Bug]: AsyncMessages.create() got an unexpected keyword argument '_function_middleware_pipeline'. #3502

@Danau5tin

Description

@Danau5tin

Description

Hey guys,
Just trying to add middleware with ChatAgent. See example below which is a slightly adjusted (using AnthropicClient) version of your example here.

Seems to be an issue with Anthropic model on FunctionInvocationContext middleware?

Code Sample

# Copyright (c) Microsoft. All rights reserved.

import asyncio
import time
from collections.abc import Awaitable, Callable
from random import randint
from typing import Annotated

from agent_framework import (
    AgentRunContext,
    ChatAgent,
    FunctionInvocationContext,
    tool,
)
from agent_framework.azure import AzureAIAgentClient
from agent_framework_anthropic import AnthropicClient
from anthropic import AsyncAnthropicFoundry
from azure.identity.aio import AzureCliCredential
from pydantic import Field


from azure.identity import DefaultAzureCredential
from azure.identity import get_bearer_token_provider


"""
Function-based Middleware Example

This sample demonstrates how to implement middleware using simple async functions instead of classes.
The example includes:

- Security middleware that validates agent requests for sensitive information
- Logging middleware that tracks function execution timing and parameters
- Performance monitoring to measure execution duration

Function-based middleware is ideal for simple, stateless operations and provides a more
lightweight approach compared to class-based middleware. Both agent and function middleware
can be implemented as async functions that accept context and next parameters.
"""


# NOTE: approval_mode="never_require" is for sample brevity. Use "always_require" in production; see samples/getting_started/tools/function_tool_with_approval.py and samples/getting_started/tools/function_tool_with_approval_and_threads.py.
@tool(approval_mode="never_require")
def get_weather(
    location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
    """Get the weather for a given location."""
    conditions = ["sunny", "cloudy", "rainy", "stormy"]
    return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."


async def security_agent_middleware(
    context: AgentRunContext,
    next: Callable[[AgentRunContext], Awaitable[None]],
) -> None:
    """Agent middleware that checks for security violations."""
    # Check for potential security violations in the query
    # For this example, we'll check the last user message
    last_message = context.messages[-1] if context.messages else None
    if last_message and last_message.text:
        query = last_message.text
        if "password" in query.lower() or "secret" in query.lower():
            print("[SecurityAgentMiddleware] Security Warning: Detected sensitive information, blocking request.")
            # Simply don't call next() to prevent execution
            return

    print("[SecurityAgentMiddleware] Security check passed.")
    await next(context)


async def logging_function_middleware(
    context: FunctionInvocationContext,
    next: Callable[[FunctionInvocationContext], Awaitable[None]],
) -> None:
    """Function middleware that logs function calls."""
    function_name = context.function.name
    print(f"[LoggingFunctionMiddleware] About to call function: {function_name}.")

    start_time = time.time()

    await next(context)

    end_time = time.time()
    duration = end_time - start_time

    print(f"[LoggingFunctionMiddleware] Function {function_name} completed in {duration:.5f}s.")

async def main() -> None:
    """Example demonstrating function-based middleware."""
    print("=== Function-based Middleware Example ===")

    # For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
    # authentication option.

    chat_client = AnthropicClient(
        anthropic_client=AsyncAnthropicFoundry(
            resource="",
            azure_ad_token_provider=get_bearer_token_provider(
                DefaultAzureCredential(),
                "",
            ),
        ),
        model_id="claude-sonnet-4-5",
    )

    agent = ChatAgent(
        chat_client=chat_client,
        name="WeatherAgent",
        instructions="You are a helpful weather assistant.",
        tools=get_weather,
        middleware=[security_agent_middleware, logging_function_middleware],
    )

    # Test with normal query
    print("\n--- Normal Query ---")
    query = "What's the weather like in Tokyo?"
    print(f"User: {query}")
    result = await agent.run(query)
    print(f"Agent: {result.text if result.text else 'No response'}\n")

    # Test with security violation
    print("--- Security Test ---")
    query = "What's the secret weather password?"
    print(f"User: {query}")
    result = await agent.run(query)
    print(f"Agent: {result.text if result.text else 'No response'}\n")


if __name__ == "__main__":
    asyncio.run(main())

Error Messages / Stack Traces

python src/a.py                                                                       
  === Function-based Middleware Example ===                                               
                                                                                          
  --- Normal Query ---                                                                    
  User: What's the weather like in Tokyo?                                                 
  [SecurityAgentMiddleware] Security check passed.                                        
  Traceback (most recent call last):                                                      
    File "src/a.py", line 130, in <module>                                                
      asyncio.run(main())                                                                 
    File ".../lib/python3.12/asyncio/runners.py", line 195, in run                        
      return runner.run(main)                                                             
             ^^^^^^^^^^^^^^^^                                                             
    File ".../lib/python3.12/asyncio/runners.py", line 118, in run                        
      return self._loop.run_until_complete(task)                                          
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                          
    File ".../lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete     
      return future.result()                                                              
             ^^^^^^^^^^^^^^^                                                              
    File "src/a.py", line 118, in main                                                    
      result = await agent.run(query)                                                     
               ^^^^^^^^^^^^^^^^^^^^^^                                                     
    File ".../site-packages/agent_framework/_middleware.py", line 1243, in                
  middleware_enabled_run                                                                  
      result = await agent_pipeline.execute(                                              
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                              
    File ".../site-packages/agent_framework/_middleware.py", line 811, in execute         
      await first_handler(context)                                                        
    File ".../site-packages/agent_framework/_middleware.py", line 686, in current_handler 
      await middleware.process(c, next_handler)                                           
    File ".../site-packages/agent_framework/_middleware.py", line 603, in process         
      await self.func(context, next)                                                      
    File "src/a.py", line 63, in security_agent_middleware                                
      await next(context)                                                                 
    File ".../site-packages/agent_framework/_middleware.py", line 676, in final_wrapper   
      result = await final_handler(c)                                                     
               ^^^^^^^^^^^^^^^^^^^^^^                                                     
    File ".../site-packages/agent_framework/_middleware.py", line 808, in                 
  agent_final_handler                                                                     
      return await final_handler(c)                                                       
             ^^^^^^^^^^^^^^^^^^^^^^                                                       
    File ".../site-packages/agent_framework/_middleware.py", line 1241, in                
  _execute_handler                                                                        
      return await original_run(self, ctx.messages, thread=thread, **ctx.kwargs)  # type: 
  ignore                                                                                  
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^          
    File ".../site-packages/agent_framework/observability.py", line 1348, in trace_run    
      return await run_func(self, messages=messages, thread=thread, **kwargs)             
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^             
    File ".../site-packages/agent_framework/_agents.py", line 842, in run                 
      response = await self.chat_client.get_response(                                     
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                     
    File ".../site-packages/agent_framework/_tools.py", line 1946, in                     
  function_invocation_wrapper                                                             
      response = await func(self, messages=prepped_messages, options=options,             
  **filtered_kwargs)                                                                      
                                                                                          
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^         
    File ".../site-packages/agent_framework/observability.py", line 1073, in              
  trace_get_response                                                                      
      return await func(                                                                  
             ^^^^^^^^^^^                                                                  
    File ".../site-packages/agent_framework/_middleware.py", line 1373, in                
  middleware_enabled_get_response                                                         
      return await original_get_response(                                                 
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                 
    File ".../site-packages/agent_framework/_clients.py", line 339, in get_response       
      return await self._inner_get_response(                                              
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                              
    File ".../site-packages/agent_framework_anthropic/_chat_client.py", line 344, in      
  _inner_get_response                                                                     
      message = await self.anthropic_client.beta.messages.create(**run_options,           
  stream=False)                                                                           
                                                                                          
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                 
  TypeError: AsyncMessages.create() got an unexpected keyword argument                    
  '_function_middleware_pipeline'

Package Versions

"agent-framework>=1.0.0b251218",     "agent-framework-azure-ai>=1.0.0b251218",

Python Version

3.12.12

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions