KeyError: Context variable not found with certain system prompts
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:
- Set an agent's
instructionto Svelte 5 small system prompt for LLMs - Run
uv run adk web - Try to use the agent
- Get KeyError: 'Context variable not found:
count.' which is raised insrc\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.
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.