Files
microsoft--agent-framework/python/samples/02-agents/context_providers
Eduard van Valkenburg 28389df805 Python: Move SessionStore to core and persist Foundry Responses sessions (#7306)
* Python: Move session persistence into core

Move SessionStore and durable msgspec-backed storage into core, restore sessions in Foundry Responses hosting with per-user isolation, and document the serialization design.

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Python: Address session persistence review feedback

Harden scoped file paths and corruption recovery, preserve session serialization compatibility, clarify dependency placement, and add reproducible benchmark evidence.

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Python: Preserve session snapshot compatibility

Deep-copy in-memory session writes and retain existing Telegram session keys so stored conversations continue resolving.

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Python: Simplify Foundry session isolation

Add experimental FoundrySessionStore backed by Agent Server request context, remove resolver plumbing, and centralize v2 user isolation for sessions, checkpoints, and approvals.

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Python: Reduce Foundry session helper layering

Inline the single-use request user accessor while keeping separate context validation, fingerprint, and directory helpers for their distinct callers.

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Python: Clarify Foundry request context validation

Separate fail-fast request validation from context retrieval so Responses no longer appears to discard a returned context.

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Python: Share Foundry request context helpers

Move protocol validation and user-scope derivation into a dedicated request-context module, leaving the session-store module focused on storage.

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Restore Foundry checkpoint storage paths

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Simplify Foundry session storage paths

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Persist Foundry sessions under hosted home

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Make hosted path test platform independent

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Address session persistence review feedback

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Isolate Foundry session path handling

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Clarify Foundry session path terminology

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Align Foundry sessions with Responses continuity

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Finalize Foundry Responses session persistence

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Add session store feature usage telemetry

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

* Fix hosted per-call history persistence

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

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c

---------

Copilot-Session: 3e5c81ad-75e8-4e92-a883-8bbd676c6c8c
2026-07-30 13:04:08 +00:00
..

Context Provider Samples

These samples demonstrate how to use context providers to enrich agent conversations with external knowledge — from custom logic to Azure AI Search (RAG) and memory services.

Samples

File / Folder Description
simple_context_provider.py Implement a custom context provider by extending ContextProvider to extract and inject structured user information across turns.
todo_provider.py Use the built-in TodoProvider to give an agent todo-list tools. A scripted walkthrough that plans multi-step work and prints the evolving todo list after each turn.
agent_mode_provider.py Use the built-in AgentModeProvider to track and switch an agent's operating mode at runtime. An interactive loop with a /mode slash command demonstrating the built-in plan/execute modes and custom modes.
cross_session_observer.py Detect injected context messages whose origins differ from the current session, via the Message.additional_properties["_attribution"]["origin_session_ids"] field. Self-contained — no LLM credentials required.
azure_ai_foundry_memory.py Use FoundryMemoryProvider to add semantic memory — automatically retrieves, searches, and stores memories via Microsoft Foundry.
file_memory_provider.py Use the built-in FileMemoryProvider with FileSystemAgentFileStore to give an agent tools for storing and recalling memories as files, and configure the scope so memories persist and are recalled across separate sessions.
file_access_data_processing/ Use FileAccessProvider with FileSystemAgentFileStore to give an agent read/write/search access to a folder of CSV data files. See its own README.
azure_ai_search/ Retrieval Augmented Generation (RAG) with Azure AI Search in semantic and agentic modes. See its own README.
azure_content_understanding/ Analyze documents, images, audio, and video with Azure Content Understanding and inject the extracted content into agent context.
mem0/ Memory-powered context using the Mem0 integration (open-source and managed). See its own README.
redis/ Redis-backed context providers for conversation memory and sessions. See its own README.

Prerequisites

For cross_session_observer.py:

  • No external dependencies; runs against in-memory SessionContext.

For simple_context_provider.py:

  • FOUNDRY_PROJECT_ENDPOINT: Your Microsoft Foundry project endpoint
  • FOUNDRY_MODEL: Model deployment name
  • Azure CLI authentication (az login)

For todo_provider.py:

  • FOUNDRY_PROJECT_ENDPOINT: Your Microsoft Foundry project endpoint
  • FOUNDRY_MODEL: Model deployment name
  • Azure CLI authentication (az login)

For agent_mode_provider.py:

  • FOUNDRY_PROJECT_ENDPOINT: Your Microsoft Foundry project endpoint
  • FOUNDRY_MODEL: Model deployment name
  • Azure CLI authentication (az login)
  • To try the custom concise/detailed modes instead of the built-in plan/execute modes, set the in-file USE_CUSTOM_MODES constant to True.
  • This sample is interactive: it reads commands from the console in a loop (type /exit to quit).

For azure_ai_foundry_memory.py:

  • FOUNDRY_PROJECT_ENDPOINT: Your Microsoft Foundry project endpoint
  • FOUNDRY_MODEL: Chat/responses model deployment name
  • AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME: Embedding model deployment name (e.g., text-embedding-ada-002)
  • Azure CLI authentication (az login)

For file_memory_provider.py:

  • FOUNDRY_PROJECT_ENDPOINT: Your Microsoft Foundry project endpoint
  • FOUNDRY_MODEL: Chat model deployment name
  • Azure CLI authentication (az login)

For file_access_data_processing/:

  • FOUNDRY_PROJECT_ENDPOINT: Your Microsoft Foundry project endpoint
  • FOUNDRY_MODEL: Chat model deployment name
  • Azure CLI authentication (az login)

See each subfolder's README for provider-specific prerequisites.