Skip to content

Commit 59970b6

Browse files
GWealecopybara-github
authored andcommitted
perf: remove state injection when instruction has no placeholders
inject_session_state runs an async regex substitution over the instruction on every LLM call to fill {state} and {artifact.x} placeholders. The pattern ({+[^{}]*}+) requires a '{', so a template that contains no '{' can never match and is returned unchanged. Static instructions with no placeholders are the common case, so short-circuit before the scan and skip the regex pass and the per-call async substitution setup. Behavior is unchanged. Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 940535088
1 parent e470548 commit 59970b6

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/google/adk/utils/instructions_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ async def build_instruction(
6565
The instruction template with values populated.
6666
"""
6767

68+
# The substitution pattern requires a '{', so a template without one can
69+
# never match. Return it as-is to avoid the regex scan on every LLM call,
70+
# which is the common case for static instructions.
71+
if '{' not in template:
72+
return template
73+
6874
invocation_context = readonly_context._invocation_context
6975

7076
async def _async_sub(pattern, repl_async_fn, string) -> str:

tests/unittests/utils/test_instructions_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ async def test_inject_session_state():
7272
assert populated_instruction == "Hello Foo, you are in active state."
7373

7474

75+
@pytest.mark.asyncio
76+
async def test_inject_session_state_without_placeholders_returns_template():
77+
instruction_template = "A static instruction with no placeholders."
78+
invocation_context = await _create_test_readonly_context(
79+
state={"user_name": "Foo"}
80+
)
81+
82+
populated_instruction = await instructions_utils.inject_session_state(
83+
instruction_template, invocation_context
84+
)
85+
assert populated_instruction == instruction_template
86+
87+
7588
@pytest.mark.asyncio
7689
async def test_inject_session_state_with_artifact():
7790
instruction_template = "The artifact content is: {artifact.my_file}"

0 commit comments

Comments
 (0)