adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

KeyError: Context variable not found with certain system prompts

Open cschmidterNBTC opened this issue 1 year ago • 1 comments

Describe the bug Using a system prompt that contains a certain escape character/substring breaks the agent initialization.

To Reproduce Steps to reproduce the behavior:

  1. Set an agent's instruction to Svelte 5 small system prompt for LLMs
  2. Run uv run adk web
  3. Try to use the agent
  4. Get KeyError: 'Context variable not found: count.' which is raised in src\google\adk\flows\llm_flows\instructions.py.

Expected behavior The agent should work as intended with the system prompt.

Desktop (please complete the following information):

  • OS: Windows
  • Python version(python -V): 3.13
  • ADK version(pip show google-adk): 0.4.0

Additional context I was able to work around this issue by Base64 encoding my system prompt. It was then able to be interpreted by the model.

cschmidterNBTC avatar May 08 '25 15:05 cschmidterNBTC

This is an known issue. In the prompt, when you have something like {count}, adk thinks you want to inject a session state called count and will throw if adk doesn't find it in session. However, in your case, it's code snippet.


To mitigate that, we have https://github.com/google/adk-python/commit/9e9dfa7472de563f005d807b79214b8099b21623 to stop injecting if you use InstructionProvider. In that case, you can retrieve the needed context from readonly_context and construct your instruction.

Jacksunwei avatar May 28 '25 07:05 Jacksunwei