Skip to content

Commit 7c5008f

Browse files
haranrkcopybara-github
authored andcommitted
refactor(agents): move InstructionProvider alias into instructions_utils
Move the InstructionProvider type alias out of llm_agent.py into the existing utils/instructions_utils.py module, and re-export it from llm_agent so existing imports (and the public API) are unaffected. This lets other agents (e.g. ManagedAgent) reuse the alias without importing the heavy llm_agent module, and colocates it with inject_session_state, the helper that consumes an InstructionProvider's output. Co-authored-by: Haran Rajkumar <haranrk@google.com> PiperOrigin-RevId: 951007235
1 parent 3367b5b commit 7c5008f

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/google/adk/agents/llm_agent.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from ..utils._schema_utils import SchemaType
6060
from ..utils._schema_utils import validate_schema
6161
from ..utils.context_utils import Aclosing
62+
from ..utils.instructions_utils import InstructionProvider as InstructionProvider
6263
from .base_agent import BaseAgent
6364
from .base_agent import BaseAgentState
6465
from .base_agent_config import BaseAgentConfig as BaseAgentConfig
@@ -130,9 +131,6 @@
130131
list[_SingleOnToolErrorCallback],
131132
]
132133

133-
InstructionProvider: TypeAlias = Callable[
134-
[ReadonlyContext], Union[str, Awaitable[str]]
135-
]
136134
ToolUnion: TypeAlias = Union[Callable, BaseTool, BaseToolset]
137135

138136

src/google/adk/utils/instructions_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,28 @@
1616

1717
import logging
1818
import re
19+
from typing import Awaitable
20+
from typing import Callable
21+
from typing import Union
22+
23+
from typing_extensions import TypeAlias
1924

2025
from ..agents.readonly_context import ReadonlyContext
2126
from ..sessions.state import State
2227

2328
__all__ = [
29+
'InstructionProvider',
2430
'inject_session_state',
2531
]
2632

2733
logger = logging.getLogger('google_adk.' + __name__)
2834

35+
# Type alias for agent instruction providers: a callable that receives a
36+
# ReadonlyContext and returns an instruction string (sync or async).
37+
InstructionProvider: TypeAlias = Callable[
38+
[ReadonlyContext], Union[str, Awaitable[str]]
39+
]
40+
2941

3042
async def inject_session_state(
3143
template: str,

tests/unittests/utils/test_instructions_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
# limitations under the License.
1414

1515
from google.adk.agents.llm_agent import Agent
16+
from google.adk.agents.llm_agent import InstructionProvider as LlmAgentInstructionProvider
1617
from google.adk.agents.readonly_context import ReadonlyContext
1718
from google.adk.sessions.session import Session
1819
from google.adk.utils import instructions_utils
20+
from google.adk.utils.instructions_utils import InstructionProvider
1921
import pytest
2022

2123
from .. import testing_utils
@@ -280,3 +282,13 @@ async def test_inject_session_state_with_optional_missing_state_returns_empty():
280282
instruction_template, invocation_context
281283
)
282284
assert populated_instruction == "Optional value: "
285+
286+
287+
def test_module_exposes_instruction_provider_alias():
288+
assert instructions_utils.InstructionProvider is InstructionProvider
289+
290+
291+
def test_llm_agent_reexports_same_instruction_provider():
292+
# Existing importers rely on `from ...llm_agent import InstructionProvider`;
293+
# it must remain the exact same object after moving the alias here.
294+
assert LlmAgentInstructionProvider is InstructionProvider

0 commit comments

Comments
 (0)