Python: [BREAKING] PR2 — Wire context provider pipeline, remove old types, update all consumers (#3850)

* PR2: Wire context provider pipeline and update all internal consumers

- Replace AgentThread with AgentSession across all packages
- Replace ContextProvider with BaseContextProvider across all packages
- Replace context_provider param with context_providers (Sequence)
- Replace thread= with session= in run() signatures
- Replace get_new_thread() with create_session()
- Add get_session(service_session_id) to agent interface
- DurableAgentThread -> DurableAgentSession
- Remove _notify_thread_of_new_messages from WorkflowAgent
- Wire before_run/after_run context provider pipeline in RawAgent
- Auto-inject InMemoryHistoryProvider when no providers configured

* fix: update all tests for context provider pipeline, fix lazy-loaders, remove old test files

* refactor: update all sample files for context provider pipeline (AgentThread→AgentSession, ContextProvider→BaseContextProvider)

* fix: update remaining ag-ui references (client docstring, getting_started sample)

* fix: make get_session service_session_id keyword-only to avoid confusion with session_id

* refactor: rename _RunContext.thread_messages to session_messages

* refactor: remove _threads.py, _memory.py, and old provider files; migrate devui to use plain message lists

* rename: remove _new_ prefix from test files

* refactor: rewrite SlidingWindowChatMessageStore as SlidingWindowHistoryProvider(InMemoryHistoryProvider)

* fix: read full history from session state directly instead of reaching into provider internals

* fix: update stale .pyi stubs, sample imports, and README references for new provider types

* fix: remove stale message_store, _notify_thread_of_new_messages, and session_id.key references in samples

* refactor: merge context_providers and sessions sample folders into sessions, remove aggregate_context_provider

* refactor: UserInfoMemory stores state in session.state instead of instance attributes

* feat: add Pydantic BaseModel support to session state serialization

Pydantic models stored in session.state are now automatically serialized
via model_dump() and restored via model_validate() during to_dict()/from_dict()
round-trips. Models are auto-registered on first serialization; use
register_state_type() for cold-start deserialization.

Also export register_state_type as a public API.

* fix mem0

* Update sample README links and descriptions for session terminology

- Replace 'thread' with 'session' in sample descriptions across all READMEs
- Update file links for renamed samples (mem0_sessions, redis_sessions, etc.)
- Fix Threads section → Sessions section in main samples/README.md
- Update tools, middleware, workflows, durabletask, azure_functions READMEs
- Update architecture diagrams in concepts/tools/README.md
- Update migration guides (autogen, semantic-kernel)

* Fix broken Redis README link to renamed sample

* Fix Mem0 OSS client search: pass scoping params as direct kwargs

AsyncMemory (OSS) expects user_id/agent_id/run_id as direct kwargs,
while AsyncMemoryClient (Platform) expects them in a filters dict.
Adds tests for both client types.

Port of fix from #3844 to new Mem0ContextProvider.

* Fix rebase issues: restore missing _conversation_state.py and checkpoint decode logic

- Add back _conversation_state.py (encode/decode_chat_messages) lost in rebase
- Fix on_checkpoint_restore to decode cache/conversation with decode_chat_messages
- Fix on_checkpoint_restore to use decode_checkpoint_value for pending requests
- Add tests/workflow/__init__.py for relative import support
- Fix test_agent_executor checkpoint selection (checkpoints[1] not superstep)

* Add STORES_BY_DEFAULT ClassVar to skip redundant InMemoryHistoryProvider injection

Chat clients that store history server-side by default (OpenAI Responses API,
Azure AI Agent) now declare STORES_BY_DEFAULT = True. The agent checks this
during auto-injection and skips InMemoryHistoryProvider unless the user
explicitly sets store=False.

* Fix broken markdown links in azure_ai and redis READMEs

* Fix getting-started samples to use session API instead of removed thread/ContextProvider API

* updates to workflow as agent

* fix group chat import

* Rename Thread→Session throughout, fix service_session_id propagation, remove stale AGUIThread

- Fix: Propagate conversation_id from ChatResponse back to session.service_session_id
  in both streaming and non-streaming paths in _agents.py
- Rename AgentThreadException → AgentSessionException
- Remove stale AGUIThread from ag_ui lazy-loader
- Rename use_service_thread → use_service_session in ag-ui package
- Rename test functions from *_thread_* to *_session_*
- Rename sample files from *_thread* to *_session*
- Update docstrings and comments: thread → session
- Update _mcp.py kwargs filter: add 'session' alongside 'thread'
- Fix ContinuationToken docstring example: thread=thread → session=session
- Fix _clients.py docstring: 'Agent threads' → 'Agent sessions'

* Fix broken markdown links after thread→session file renames

* fix azure ai test
This commit is contained in:
Eduard van Valkenburg
2026-02-12 22:00:32 +01:00
committed by GitHub
Unverified
parent 0c67dbbce5
commit 1e350ea22f
312 changed files with 6669 additions and 11423 deletions
@@ -29,7 +29,7 @@ from collections.abc import Awaitable, Callable, Sequence
from dataclasses import dataclass
from typing import Any, ClassVar, cast
from agent_framework import Agent, AgentThread, Message, SupportsAgentRun
from agent_framework import Agent, AgentSession, Message, SupportsAgentRun
from agent_framework._workflows._agent_executor import AgentExecutor, AgentExecutorRequest, AgentExecutorResponse
from agent_framework._workflows._agent_utils import resolve_agent_id
from agent_framework._workflows._checkpoint import CheckpointStorage
@@ -291,7 +291,7 @@ class AgentBasedGroupChatOrchestrator(BaseGroupChatOrchestrator):
max_rounds: int | None = None,
termination_condition: TerminationCondition | None = None,
retry_attempts: int | None = None,
thread: AgentThread | None = None,
session: AgentSession | None = None,
) -> None:
"""Initialize the GroupChatOrchestrator.
@@ -302,7 +302,7 @@ class AgentBasedGroupChatOrchestrator(BaseGroupChatOrchestrator):
max_rounds: Optional limit on selection rounds to prevent infinite loops.
termination_condition: Optional callable that halts the conversation when it returns True
retry_attempts: Optional number of retry attempts for the agent in case of failure.
thread: Optional agent thread to use for the orchestrator agent.
session: Optional agent session to use for the orchestrator agent.
"""
super().__init__(
resolve_agent_id(agent),
@@ -313,7 +313,7 @@ class AgentBasedGroupChatOrchestrator(BaseGroupChatOrchestrator):
)
self._agent = agent
self._retry_attempts = retry_attempts
self._thread = thread or agent.get_new_thread()
self._session = session or agent.create_session()
# Cache for messages since last agent invocation
# This is different from the full conversation history maintained by the base orchestrator
self._cache: list[Message] = []
@@ -471,7 +471,7 @@ class AgentBasedGroupChatOrchestrator(BaseGroupChatOrchestrator):
# Run the agent in non-streaming mode for simplicity
agent_response = await self._agent.run(
messages=conversation,
thread=self._thread,
session=self._session,
options={"response_format": AgentOrchestrationOutput},
)
# Parse and validate the structured output
@@ -547,8 +547,8 @@ class AgentBasedGroupChatOrchestrator(BaseGroupChatOrchestrator):
"""Capture current orchestrator state for checkpointing."""
state = await super().on_checkpoint_save()
state["cache"] = self._cache
serialized_thread = await self._thread.serialize()
state["thread"] = serialized_thread
serialized_session = self._session.to_dict()
state["session"] = serialized_session
return state
@@ -557,9 +557,9 @@ class AgentBasedGroupChatOrchestrator(BaseGroupChatOrchestrator):
"""Restore executor state from checkpoint."""
await super().on_checkpoint_restore(state)
self._cache = state.get("cache", [])
serialized_thread = state.get("thread")
if serialized_thread:
self._thread = await self._agent.deserialize_thread(serialized_thread)
serialized_session = state.get("session")
if serialized_session:
self._session = AgentSession.from_dict(serialized_session)
# endregion
@@ -38,7 +38,7 @@ from typing import Any, cast
from agent_framework import Agent, SupportsAgentRun
from agent_framework._middleware import FunctionInvocationContext, FunctionMiddleware
from agent_framework._threads import AgentThread
from agent_framework._sessions import AgentSession
from agent_framework._tools import FunctionTool, tool
from agent_framework._types import AgentResponse, AgentResponseUpdate, Message
from agent_framework._workflows._agent_executor import AgentExecutor, AgentExecutorRequest, AgentExecutorResponse
@@ -196,7 +196,7 @@ class HandoffAgentExecutor(AgentExecutor):
agent: SupportsAgentRun,
handoffs: Sequence[HandoffConfiguration],
*,
agent_thread: AgentThread | None = None,
agent_session: AgentSession | None = None,
is_start_agent: bool = False,
termination_condition: TerminationCondition | None = None,
autonomous_mode: bool = False,
@@ -208,7 +208,7 @@ class HandoffAgentExecutor(AgentExecutor):
Args:
agent: The agent to execute
handoffs: Sequence of handoff configurations defining target agents
agent_thread: Optional AgentThread that manages the agent's execution context
agent_session: Optional AgentSession that manages the agent's execution context
is_start_agent: Whether this agent is the starting agent in the handoff workflow.
There can only be one starting agent in a handoff workflow.
termination_condition: Optional callable that determines when to terminate the workflow
@@ -222,7 +222,7 @@ class HandoffAgentExecutor(AgentExecutor):
autonomous_mode_turn_limit: Maximum number of autonomous turns before requesting user input.
"""
cloned_agent = self._prepare_agent_with_handoffs(agent, handoffs)
super().__init__(cloned_agent, agent_thread=agent_thread)
super().__init__(cloned_agent, session=agent_session)
self._handoff_targets = {handoff.target_id for handoff in handoffs}
self._termination_condition = termination_condition
@@ -306,8 +306,7 @@ class HandoffAgentExecutor(AgentExecutor):
id=agent.id,
name=agent.name,
description=agent.description,
chat_message_store_factory=agent.chat_message_store_factory,
context_provider=agent.context_provider,
context_providers=agent.context_providers,
middleware=middleware,
default_options=cloned_options, # type: ignore[arg-type]
)
@@ -1338,8 +1338,8 @@ class MagenticAgentExecutor(AgentExecutor):
# Request into related
self._pending_agent_requests.clear()
self._pending_responses_to_agent.clear()
# Reset threads
self._agent_thread = self._agent.get_new_thread()
# Reset sessions
self._agent_thread = self._agent.create_session()
# endregion Magentic Agent Executor
@@ -9,7 +9,7 @@ from agent_framework import (
AgentExecutorResponse,
AgentResponse,
AgentResponseUpdate,
AgentThread,
AgentSession,
BaseAgent,
ChatResponse,
ChatResponseUpdate,
@@ -41,7 +41,7 @@ class StubAgent(BaseAgent):
messages: str | Message | Sequence[str | Message] | None = None,
*,
stream: bool = False,
thread: AgentThread | None = None,
session: AgentSession | None = None,
**kwargs: Any,
) -> Awaitable[AgentResponse] | AsyncIterable[AgentResponseUpdate]:
if stream:
@@ -78,7 +78,7 @@ class StubManagerAgent(Agent):
self,
messages: str | Message | Sequence[str | Message] | None = None,
*,
thread: AgentThread | None = None,
session: AgentSession | None = None,
**kwargs: Any,
) -> AgentResponse:
if self._call_count == 0:
@@ -132,7 +132,7 @@ class ConcatenatedJsonManagerAgent(Agent):
self,
messages: str | Message | Sequence[str | Message] | None = None,
*,
thread: AgentThread | None = None,
session: AgentSession | None = None,
**kwargs: Any,
) -> AgentResponse:
if self._call_count == 0:
@@ -346,7 +346,7 @@ class TestGroupChatBuilder:
super().__init__(name="", description="test")
def run(
self, messages: Any = None, *, stream: bool = False, thread: Any = None, **kwargs: Any
self, messages: Any = None, *, stream: bool = False, session: Any = None, **kwargs: Any
) -> AgentResponse | AsyncIterable[AgentResponseUpdate]:
if stream:
@@ -898,7 +898,7 @@ async def test_group_chat_with_orchestrator_factory_returning_chat_agent():
self,
messages: str | Message | Sequence[str | Message] | None = None,
*,
thread: AgentThread | None = None,
session: AgentSession | None = None,
**kwargs: Any,
) -> AgentResponse:
if self._call_count == 0:
@@ -7,11 +7,10 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from agent_framework import (
Agent,
BaseContextProvider,
ChatResponse,
ChatResponseUpdate,
Content,
Context,
ContextProvider,
Message,
ResponseStream,
WorkflowEvent,
@@ -306,16 +305,18 @@ async def test_tool_choice_preserved_from_agent_config():
async def test_context_provider_preserved_during_handoff():
"""Verify that context_provider is preserved when cloning agents in handoff workflows."""
"""Verify that context_providers are preserved when cloning agents in handoff workflows."""
# Track whether context provider methods were called
provider_calls: list[str] = []
class TestContextProvider(ContextProvider):
class TestContextProvider(BaseContextProvider):
"""A test context provider that tracks its invocations."""
async def invoking(self, messages: Sequence[Message], **kwargs: Any) -> Context:
provider_calls.append("invoking")
return Context(instructions="Test context from provider.")
def __init__(self) -> None:
super().__init__("test")
async def before_run(self, **kwargs: Any) -> None:
provider_calls.append("before_run")
# Create context provider
context_provider = TestContextProvider()
@@ -328,13 +329,13 @@ async def test_context_provider_preserved_during_handoff():
client=mock_client,
name="test_agent",
id="test_agent",
context_provider=context_provider,
context_providers=[context_provider],
)
# Verify the original agent has the context provider
assert agent.context_provider is context_provider, "Original agent should have context provider"
assert context_provider in agent.context_providers, "Original agent should have context provider"
# Build handoff workflow - this should clone the agent and preserve context_provider
# Build handoff workflow - this should clone the agent and preserve context_providers
workflow = HandoffBuilder(participants=[agent]).with_start_agent(agent).build()
# Run workflow with a simple message to trigger context provider
@@ -9,7 +9,7 @@ import pytest
from agent_framework import (
AgentResponse,
AgentResponseUpdate,
AgentThread,
AgentSession,
BaseAgent,
Content,
Executor,
@@ -153,7 +153,7 @@ class StubAgent(BaseAgent):
messages: str | Message | Sequence[str | Message] | None = None,
*,
stream: bool = False,
thread: AgentThread | None = None,
session: AgentSession | None = None,
**kwargs: Any,
) -> Awaitable[AgentResponse] | AsyncIterable[AgentResponseUpdate]:
if stream:
@@ -414,7 +414,7 @@ class StubManagerAgent(BaseAgent):
messages: str | Message | Sequence[str | Message] | None = None,
*,
stream: bool = False,
thread: Any = None,
session: Any = None,
**kwargs: Any,
) -> Awaitable[AgentResponse] | AsyncIterable[AgentResponseUpdate]:
if stream:
@@ -526,7 +526,7 @@ class StubThreadAgent(BaseAgent):
def __init__(self, name: str | None = None) -> None:
super().__init__(name=name or "agentA")
def run(self, messages=None, *, stream: bool = False, thread=None, **kwargs): # type: ignore[override]
def run(self, messages=None, *, stream: bool = False, session=None, **kwargs): # type: ignore[override]
if stream:
return self._run_stream()
@@ -554,7 +554,7 @@ class StubAssistantsAgent(BaseAgent):
super().__init__(name="agentA")
self.client = StubAssistantsClient() # type name contains 'AssistantsClient'
def run(self, messages=None, *, stream: bool = False, thread=None, **kwargs): # type: ignore[override]
def run(self, messages=None, *, stream: bool = False, session=None, **kwargs): # type: ignore[override]
if stream:
return self._run_stream()
@@ -10,7 +10,7 @@ import pytest
from agent_framework import (
AgentResponse,
AgentResponseUpdate,
AgentThread,
AgentSession,
Message,
SupportsAgentRun,
)
@@ -203,7 +203,7 @@ class _TestAgent:
messages: str | Message | list[str] | list[Message] | None = None,
*,
stream: bool = False,
thread: AgentThread | None = None,
thread: AgentSession | None = None,
**kwargs: Any,
) -> AgentResponse | AsyncIterable[AgentResponseUpdate]:
"""Dummy run method."""
@@ -214,9 +214,9 @@ class _TestAgent:
async def _run_stream_impl(self) -> AsyncIterable[AgentResponseUpdate]:
yield AgentResponseUpdate(messages=[Message(role="assistant", text="Test response stream")])
def get_new_thread(self, **kwargs: Any) -> AgentThread:
"""Creates a new conversation thread for the agent."""
return AgentThread(**kwargs)
def create_session(self, **kwargs: Any) -> AgentSession:
"""Creates a new conversation session for the agent."""
return AgentSession(**kwargs)
class TestAgentApprovalExecutor:
@@ -8,7 +8,7 @@ from agent_framework import (
AgentExecutorResponse,
AgentResponse,
AgentResponseUpdate,
AgentThread,
AgentSession,
BaseAgent,
Content,
Executor,
@@ -30,7 +30,7 @@ class _EchoAgent(BaseAgent):
messages: str | Message | list[str] | list[Message] | None = None,
*,
stream: bool = False,
thread: AgentThread | None = None,
session: AgentSession | None = None,
**kwargs: Any,
) -> Awaitable[AgentResponse] | AsyncIterable[AgentResponseUpdate]:
if stream: