Python: [Bug]: Request preparation consumes SessionContext.options before ContextProvider.after_run
Observed Behavior
A context provider cannot read the original run metadata when it saves a completed expense. I was using an expense agent to record a notebook purchase for 20, with GBP supplied by my checkout UI in Agent.run metadata. The provider saves the currency after the run completes. It sees the metadata missing, records the default USD, and the next conversation turn incorrectly says the purchase was saved in USD.
Expected Behavior
SessionContext.options should keep the run's currency='GBP' metadata available through after_run, so the saved expense and the next answer both use GBP.
Steps to Reproduce
- Configure a LLM mode.
- Run the python code I provided.
Minimal Reproduction
import asyncio
import os
from agent_framework import Agent, ContextProvider
from agent_framework.openai import OpenAIChatCompletionClient
class ExpenseCurrency(ContextProvider):
async def before_run(self, *, agent, session, context, state):
if "currency" in state:
context.extend_instructions(self, f"The saved notebook purchase currency is {state['currency']}.")
async def after_run(self, *, agent, session, context, state):
if "currency" not in state:
state["currency"] = context.options.get("metadata", {}).get("currency", "USD")
async def main():
agent = Agent(
client=OpenAIChatCompletionClient(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com/v1", model="deepseek-flash"),
instructions="Help me record purchases and answer questions about my saved expenses.",
context_providers=[ExpenseCurrency("expenses")],
default_options={"extra_body": {"thinking": {"type": "disabled"}}},
)
session = agent.create_session()
await agent.run("Record my notebook purchase for 20. Reply noted.", session=session, options={"metadata": {"currency": "GBP"}})
result = await agent.run("Which currency did you save for my notebook purchase? Reply only with the currency code.", session=session)
print(result.text)
assert result.text.strip() == "GBP", "The checkout supplied GBP, but the saved expense lost its currency."
asyncio.run(main())
Error Messages and Stack Traces
USD
Traceback (most recent call last):
File "/tmp/unified-context-audit/microsoft-agent-framework/investigation/after-run-options-consumed/main.py", line 30, in <module>
asyncio.run(main())
File "/tmp/unified-context-audit/microsoft-agent-framework/conda-env/lib/python3.11/asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/tmp/unified-context-audit/microsoft-agent-framework/conda-env/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/unified-context-audit/microsoft-agent-framework/conda-env/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/tmp/unified-context-audit/microsoft-agent-framework/investigation/after-run-options-consumed/main.py", line 28, in main
assert result.text.strip() == "GBP", "The checkout supplied GBP, but the saved expense lost its currency."
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: The checkout supplied GBP, but the saved expense lost its currency.
Package Versions
agent-framework-core 1.25.0; agent-framework-openai 1.18.1
Python Version
3.11.15
Operating System
Linux
Regression
Unknown
Additional Context
No response
Acknowledgements
- [x] I searched existing issues and did not find a duplicate.
- [x] I personally verified this behavior and the reproduction details are authentic.
- [x] I will wait for explicit maintainer agreement before starting implementation of a non-trivial change.
I could not reproduce this on the current main (e2f52bc) with a local fake chat client.
An Agent with a ContextProvider like yours saw context.options["metadata"] == {"currency": "GBP"} in after_run. The provider saved state["currency"] == "GBP", and the next turn's before_run added "currency is GBP" to the model request. Both tests passed. The code that builds SessionContext.options is in _agents.py, and it keeps the caller's metadata there.
This does not rule out a bug in 1.25.0. I did not install that release, and I did not run the DeepSeek client. Details are in the run.
To narrow it down, could you:
- Print
session.state["expenses"]andcontext.optionsinsideafter_runafter the first run. That shows whetherUSDwas actually saved or whether the model ignored the instruction. - Say whether the same result happens on the latest released
agent-framework-core. - Print the instructions the model receives on the second turn.
Support boundary: Direct support. I couldn't find a maintained spec or doc that sets it, so a maintainer should confirm the contract; SessionContext documents options as read-only for providers.
Generated by Issue Routing for #10384 · copilot · auto · ~46 AIC · ⌖ ~1 AIC · ⊞ 5.2K · ◷
I could not reproduce this on the current
main(e2f52bc) with a local fake chat client.An
Agentwith aContextProviderlike yours sawcontext.options["metadata"] == {"currency": "GBP"}inafter_run. The provider savedstate["currency"] == "GBP", and the next turn'sbefore_runadded "currency is GBP" to the model request. Both tests passed. The code that buildsSessionContext.optionsis in_agents.py, and it keeps the caller'smetadatathere.This does not rule out a bug in 1.25.0. I did not install that release, and I did not run the DeepSeek client. Details are in the run.
To narrow it down, could you:
session.state["expenses"]andcontext.optionsinsideafter_runafter the first run. That shows whetherUSDwas actually saved or whether the model ignored the instruction.- Say whether the same result happens on the latest released
agent-framework-core.- Print the instructions the model receives on the second turn.
Support boundary: Direct support. I couldn't find a maintained spec or doc that sets it, so a maintainer should confirm the contract;
SessionContextdocumentsoptionsas read-only for providers.Generated by Issue Routing for #10384 · copilot · auto · ~46 AIC · ⌖ ~1 AIC · ⊞ 5.2K · ◷
I retested on the latest version (1.26.0) and confirmed the bug is fixed. Thanks for checking. So, may a maintainer help close this issue?
I agree, I will close this issue, thanks for @UnZHGPPT