Skip to content

Python: [Bug]: Send Conversation History to Foundry Hosted Agent #5415

@ashishmundra4001

Description

@ashishmundra4001

Description

Earlier when I was using AgentFramework with Hosting Adapter Library
"azure-ai-agentserver-agentframework==1.0.0b17", "agent-framework==1.0.0rc3"

The input to my Agent was like below. I was sending Conversation History with each request. I was doing so because Foundry Hosted Agents do not store Conversation History yet (only Prompt Agents in Foundry do so - unless it recently changed). So we store Conversation History in our UI that calls this Agent and pass it everytime.

Now I upgraded to agent-framework1.1.0 and removed azure-ai-agentserver-agentframework and using agent-framework-foundry-hosting instead but now with this same input below in my middleware how do I read the messages? Has this input changed drastically now? How do I now pass conversation history if that is maintained outside the system and how do I pass metadata?

Previous Input to http://localhost:8088/responses that worked earlier -
{
"agent": {
"name": "My-Agent-v2",
"type": "agent_reference"
},
"stream": false,
"input": [
{"type": "message", "role": "user", "content": "eydAAPIKEYCLIENTTOKEN8"},
{"type": "message", "role": "user", "content": "Arrival time of Bus at IT."},
{"type": "message", "role": "assistant", "content": "in next 5 mins"},
{"type": "message", "role": "user", "content": "KRB."}
],
"metadata": {
"latitude": "1.2976",
"longitude": "103.7767",
"traceparent": "00-a0a00417202600001234cafeface5677-a0a0041720260001-01"
}
}

The issue is that when this message would come, earlier in my Middleware, I would read AgentContext messages and pop first message as it has tokens and set context.kwargs from that first message. This was because azure-ai-agentserver-agentframework used to truncate Authorization Headers and so there was no way for me to pass these tokens. Now in my middleware, I only see single message with text as ''.

My middleware code is like below -
class UserContextAgentMiddleware(AgentMiddleware):

async def process(
    self,
    context: AgentContext,
    next: Callable[[], Awaitable[None]],
) -> None:
    
    # Initialize trace context token for cleanup
    trace_context_token = None
            
    # Extract auth, apikey, and unitoken from message content if it has XML format
    if context.messages and len(context.messages) > 0:
        user_message = context.messages.pop(0)  # Get first message as it has tokens
        content = user_message.text     

        # Add non sensitive data to kwargs
        if hasattr(context.agent, '_request_headers'):
            request_data = context.agent._request_headers

            # Runtime values that will be forwarded to tools
            if 'latitude' in request_data:
                context.kwargs.setdefault("latitude", request_data['latitude'])
            
            if 'longitude' in request_data:
                context.kwargs.setdefault("longitude", request_data['longitude'])

            # Handle trace context setup
            if 'traceparent' in request_data:
                traceparent = request_data['traceparent']

Code Sample

This is how I am using new library to Host Agent

from agent_framework_foundry_hosting import ResponsesHostServer

        agent = await agent_factory.create_agent()
        
        # Create and return server
        server = ResponsesHostServer(agent)
        return server

Error Messages / Stack Traces

Package Versions

agent-framework==1.1.0 agent-framework-foundry-hosting

Python Version

Python 3.12

Additional Context

Additional Questions:

  • Does agent-framework-foundry-hosting truncate any headers like Authorization which was happening when using azure-ai-agentserver-agentframework? Or can I now pass Authorization header and other headers just like normal http headers?
  • What is difference between Foundry Hosted Agent Invocations and Responses API? Can documentation be updated and sample include passing metadata and headers to these APIs and passing conversation history when its maintained outside system?
  • Does Foundry Hosted Agent now support storing conversation history in its associated CosmosDB for Hosted Agents as well?

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