mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
67ce1baecf
* fix: strip function_call and text_reasoning from cross-agent workflow handoff When a reasoning model (e.g. gpt-5-mini) runs as Agent 1 in a workflow, its response includes text_reasoning items (with server-scoped IDs like rs_XXXX) and function_call items. Forwarding these to Agent 2 in a fresh conversation caused API errors because the reasoning/call IDs are scoped to the original stored response context. Changes: - Strip 'function_call', 'text_reasoning', 'function_approval_request', and 'function_approval_response' from handoff messages in _agent_executor.py - Keep 'function_result' so the actual tool output content is preserved for the next agent's context - Update unit tests to reflect that function_result messages survive handoff (messages grow from 2→3: user, tool(result), assistant(summary)) - Fix incorrect test assertions in test_function_invocation_stop_clears_* that assumed the client layer updates session.service_session_id - Also fixed _extract_function_calls to search all messages with call_id deduplication, and the error-limit stop path to submit function_call_output items before halting (via tool_choice=none cleanup call) Relates to: https://github.com/microsoft/agent-framework/issues/4047 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: reasoning model workflow handoff and history serialization Fixes multiple related issues when using reasoning models (gpt-5-mini, gpt-5.2) in multi-agent workflows that chain agents via from_response or replay full conversation history via AgentExecutorRequest. ## Reasoning items always emitted on output_item.added When a reasoning model produces encrypted or hidden reasoning (no visible text), the Responses API still fires a reasoning output item without any reasoning_text.delta events. Previously no text_reasoning Content was emitted in that case, making it invisible to downstream logic. Both the non-streaming (_parse_response_from_openai) and streaming (output_item.added) paths now always emit at least one text_reasoning Content — with empty text if no content is available — so co-occurrence detection and serialization guards work reliably. ## Reasoning items only serialized when paired with a function_call The Responses API only accepts reasoning items in input when they directly preceded a function_call in the original response. Sending a reasoning item that preceded a text response (no tool call) causes: "reasoning was provided without its required following item" _prepare_message_for_openai now checks has_function_call per message and skips text_reasoning serialization when there is no accompanying function_call. ## summary field is an array, not an object The reasoning item summary field sent to the Responses API must be an array of objects ([{"type": "summary_text", "text": ...}]), not a single object. Fixed _prepare_content_for_openai accordingly. ## service_session_id cleared when explicit history is provided When a workflow coordinator replays a full conversation (including function calls from a previous agent run) back to an executor via AgentExecutorRequest or from_response, the executor's session still held a service_session_id (previous_response_id) from the prior run. The API then received the same function-call items twice — once from previous_response_id (server-stored) and once from the explicit input — causing: "Duplicate item found with id fc_...". AgentExecutor.run (when should_respond=True) and from_response now reset self._session.service_session_id = None before running so that explicit input is the sole source of conversation context. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * small improvements in text reasoning * refactor: add reset_service_session to AgentExecutorRequest for explicit history replay Replace the implicit 'always clear service_session_id when should_respond=True' with an explicit opt-in field on AgentExecutorRequest. The old approach used should_respond=True as a proxy for 'full history replay', but that conflates two distinct intents: - Orchestrations group chat sends should_respond=True with an empty/single-message list (not a full replay) — unnecessarily clearing service_session_id. - HITL / feedback coordinators send the full prior conversation and truly need a fresh service session ID to avoid duplicate-item API errors. Changes: - Add AgentExecutorRequest.reset_service_session: bool = False - AgentExecutor.run only clears service_session_id when this flag is True - AgentExecutor.from_response unchanged (always clears; always full conversation) - Set reset_service_session=True in all full-history-replay call sites: agents_with_HITL.py, azure_chat_agents_tool_calls_with_feedback.py, autogen-migration round-robin coordinator, tau2 runner - Update _FullHistoryReplayCoordinator test helper to pass the flag Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * comment update * fixes from feedback * fix test * reverted changes to agent executor * fix: remove reset_service_session from tau2 runner Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * two other reverts * fix sample --------- Co-authored-by: Giles Odigwe <79032838+giles17@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
266 lines
8.2 KiB
Python
266 lines
8.2 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import asyncio
|
|
import os
|
|
from uuid import uuid4
|
|
|
|
from agent_framework import AgentSession
|
|
from agent_framework.openai import OpenAIChatClient
|
|
from agent_framework.redis import RedisHistoryProvider
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables from .env file
|
|
load_dotenv()
|
|
|
|
"""
|
|
Redis History Provider Session Example
|
|
|
|
This sample demonstrates how to use Redis as a history provider for session
|
|
management, enabling persistent conversation history storage across sessions
|
|
with Redis as the backend data store.
|
|
"""
|
|
|
|
# Default Redis URL for local Redis Stack.
|
|
# Override via the REDIS_URL environment variable for remote or authenticated instances.
|
|
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")
|
|
|
|
|
|
async def example_manual_memory_store() -> None:
|
|
"""Basic example of using Redis history provider."""
|
|
print("=== Basic Redis History Provider Example ===")
|
|
|
|
# Create Redis history provider
|
|
redis_provider = RedisHistoryProvider(
|
|
source_id="redis_basic_chat",
|
|
redis_url=REDIS_URL,
|
|
)
|
|
|
|
# Create agent with Redis history provider
|
|
agent = OpenAIChatClient().as_agent(
|
|
name="RedisBot",
|
|
instructions="You are a helpful assistant that remembers our conversation using Redis.",
|
|
context_providers=[redis_provider],
|
|
)
|
|
|
|
# Create session
|
|
session = agent.create_session()
|
|
|
|
# Have a conversation
|
|
print("\n--- Starting conversation ---")
|
|
query1 = "Hello! My name is Alice and I love pizza."
|
|
print(f"User: {query1}")
|
|
response1 = await agent.run(query1, session=session)
|
|
print(f"Agent: {response1.text}")
|
|
|
|
query2 = "What do you remember about me?"
|
|
print(f"User: {query2}")
|
|
response2 = await agent.run(query2, session=session)
|
|
print(f"Agent: {response2.text}")
|
|
|
|
print("Done\n")
|
|
|
|
|
|
async def example_user_session_management() -> None:
|
|
"""Example of managing user sessions with Redis."""
|
|
print("=== User Session Management Example ===")
|
|
|
|
user_id = "alice_123"
|
|
session_id = f"session_{uuid4()}"
|
|
|
|
# Create Redis history provider for specific user session
|
|
redis_provider = RedisHistoryProvider(
|
|
source_id=f"redis_{user_id}",
|
|
redis_url=REDIS_URL,
|
|
max_messages=10, # Keep only last 10 messages
|
|
)
|
|
|
|
# Create agent with history provider
|
|
agent = OpenAIChatClient().as_agent(
|
|
name="SessionBot",
|
|
instructions="You are a helpful assistant. Keep track of user preferences.",
|
|
context_providers=[redis_provider],
|
|
)
|
|
|
|
# Start conversation
|
|
session = agent.create_session(session_id=session_id)
|
|
|
|
print(f"Started session for user {user_id}")
|
|
|
|
# Simulate conversation
|
|
queries = [
|
|
"Hi, I'm Alice and I prefer vegetarian food.",
|
|
"What restaurants would you recommend?",
|
|
"I also love Italian cuisine.",
|
|
"Can you remember my food preferences?",
|
|
]
|
|
|
|
for i, query in enumerate(queries, 1):
|
|
print(f"\n--- Message {i} ---")
|
|
print(f"User: {query}")
|
|
response = await agent.run(query, session=session)
|
|
print(f"Agent: {response.text}")
|
|
|
|
print("Done\n")
|
|
|
|
|
|
async def example_conversation_persistence() -> None:
|
|
"""Example of conversation persistence across application restarts."""
|
|
print("=== Conversation Persistence Example ===")
|
|
|
|
# Phase 1: Start conversation
|
|
print("--- Phase 1: Starting conversation ---")
|
|
redis_provider = RedisHistoryProvider(
|
|
source_id="redis_persistent_chat",
|
|
redis_url=REDIS_URL,
|
|
)
|
|
|
|
agent = OpenAIChatClient().as_agent(
|
|
name="PersistentBot",
|
|
instructions="You are a helpful assistant. Remember our conversation history.",
|
|
context_providers=[redis_provider],
|
|
)
|
|
|
|
session = agent.create_session()
|
|
|
|
# Start conversation
|
|
query1 = "Hello! I'm working on a Python project about machine learning."
|
|
print(f"User: {query1}")
|
|
response1 = await agent.run(query1, session=session)
|
|
print(f"Agent: {response1.text}")
|
|
|
|
query2 = "I'm specifically interested in neural networks."
|
|
print(f"User: {query2}")
|
|
response2 = await agent.run(query2, session=session)
|
|
print(f"Agent: {response2.text}")
|
|
|
|
# Serialize session state
|
|
serialized = session.to_dict()
|
|
|
|
# Phase 2: Resume conversation (simulating app restart)
|
|
print("\n--- Phase 2: Resuming conversation (after 'restart') ---")
|
|
restored_session = AgentSession.from_dict(serialized)
|
|
|
|
# Continue conversation - agent should remember context
|
|
query3 = "What was I working on before?"
|
|
print(f"User: {query3}")
|
|
response3 = await agent.run(query3, session=restored_session)
|
|
print(f"Agent: {response3.text}")
|
|
|
|
query4 = "Can you suggest some Python libraries for neural networks?"
|
|
print(f"User: {query4}")
|
|
response4 = await agent.run(query4, session=restored_session)
|
|
print(f"Agent: {response4.text}")
|
|
|
|
print("Done\n")
|
|
|
|
|
|
async def example_session_serialization() -> None:
|
|
"""Example of session state serialization and deserialization."""
|
|
print("=== Session Serialization Example ===")
|
|
|
|
redis_provider = RedisHistoryProvider(
|
|
source_id="redis_serialization_chat",
|
|
redis_url=REDIS_URL,
|
|
)
|
|
|
|
agent = OpenAIChatClient().as_agent(
|
|
name="SerializationBot",
|
|
instructions="You are a helpful assistant.",
|
|
context_providers=[redis_provider],
|
|
)
|
|
|
|
session = agent.create_session()
|
|
|
|
# Have initial conversation
|
|
print("--- Initial conversation ---")
|
|
query1 = "Hello! I'm testing serialization."
|
|
print(f"User: {query1}")
|
|
response1 = await agent.run(query1, session=session)
|
|
print(f"Agent: {response1.text}")
|
|
|
|
# Serialize session state
|
|
serialized = session.to_dict()
|
|
print(f"\nSerialized session state: {serialized}")
|
|
|
|
# Deserialize session state (simulating loading from database/file)
|
|
print("\n--- Deserializing session state ---")
|
|
restored_session = AgentSession.from_dict(serialized)
|
|
|
|
# Continue conversation with restored session
|
|
query2 = "Do you remember what I said about testing?"
|
|
print(f"User: {query2}")
|
|
response2 = await agent.run(query2, session=restored_session)
|
|
print(f"Agent: {response2.text}")
|
|
|
|
print("Done\n")
|
|
|
|
|
|
async def example_message_limits() -> None:
|
|
"""Example of automatic message trimming with limits."""
|
|
print("=== Message Limits Example ===")
|
|
|
|
# Create provider with small message limit
|
|
redis_provider = RedisHistoryProvider(
|
|
source_id="redis_limited_chat",
|
|
redis_url=REDIS_URL,
|
|
max_messages=3, # Keep only 3 most recent messages
|
|
)
|
|
|
|
agent = OpenAIChatClient().as_agent(
|
|
name="LimitBot",
|
|
instructions="You are a helpful assistant with limited memory.",
|
|
context_providers=[redis_provider],
|
|
)
|
|
|
|
session = agent.create_session()
|
|
|
|
# Send multiple messages to test trimming
|
|
messages = [
|
|
"Message 1: Hello!",
|
|
"Message 2: How are you?",
|
|
"Message 3: What's the weather?",
|
|
"Message 4: Tell me a joke.",
|
|
"Message 5: This should trigger trimming.",
|
|
]
|
|
|
|
for i, query in enumerate(messages, 1):
|
|
print(f"\n--- Sending message {i} ---")
|
|
print(f"User: {query}")
|
|
response = await agent.run(query, session=session)
|
|
print(f"Agent: {response.text}")
|
|
|
|
print("Done\n")
|
|
|
|
|
|
async def main() -> None:
|
|
"""Run all Redis history provider examples."""
|
|
print("Redis History Provider Examples")
|
|
print("=" * 50)
|
|
print("Prerequisites:")
|
|
print("- Redis server running (set REDIS_URL env var or default localhost:6379)")
|
|
print("- OPENAI_API_KEY environment variable set")
|
|
print("=" * 50)
|
|
|
|
# Check prerequisites
|
|
if not os.getenv("OPENAI_API_KEY"):
|
|
print("ERROR: OPENAI_API_KEY environment variable not set")
|
|
return
|
|
|
|
try:
|
|
# Run all examples
|
|
await example_manual_memory_store()
|
|
await example_user_session_management()
|
|
await example_conversation_persistence()
|
|
await example_session_serialization()
|
|
await example_message_limits()
|
|
|
|
print("All examples completed successfully!")
|
|
|
|
except Exception as e:
|
|
print(f"Error running examples: {e}")
|
|
raise
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|