|
37 | 37 | from pydantic import BaseModel |
38 | 38 | import pytest |
39 | 39 | from pytest import mark |
| 40 | +from google.adk.events.event import Event |
40 | 41 |
|
41 | 42 | from .. import testing_utils |
42 | 43 |
|
@@ -943,6 +944,92 @@ class CustomOutput(BaseModel): |
943 | 944 | } |
944 | 945 |
|
945 | 946 |
|
| 947 | +@mark.asyncio |
| 948 | +async def test_run_async_uses_code_execution_result_output_when_no_text( |
| 949 | + monkeypatch, |
| 950 | +): |
| 951 | + """Verify run_async falls back to code execution output when text is empty.""" |
| 952 | + |
| 953 | + from google.adk.events.event import Event |
| 954 | + |
| 955 | + async def _event_stream(): |
| 956 | + yield Event( |
| 957 | + author='tool_agent', |
| 958 | + content=types.Content( |
| 959 | + parts=[ |
| 960 | + types.Part( |
| 961 | + code_execution_result=types.CodeExecutionResult( |
| 962 | + output='IRR=0.1472', |
| 963 | + outcome=types.Outcome.OUTCOME_OK, |
| 964 | + ) |
| 965 | + ) |
| 966 | + ] |
| 967 | + ), |
| 968 | + ) |
| 969 | + |
| 970 | + class StubRunner: |
| 971 | + |
| 972 | + def __init__( |
| 973 | + self, |
| 974 | + *, |
| 975 | + app_name: str, |
| 976 | + agent: Agent, |
| 977 | + artifact_service, |
| 978 | + session_service, |
| 979 | + memory_service, |
| 980 | + credential_service, |
| 981 | + plugins, |
| 982 | + ): |
| 983 | + del app_name, agent, artifact_service, memory_service |
| 984 | + del credential_service, plugins |
| 985 | + self.session_service = session_service |
| 986 | + |
| 987 | + def run_async( |
| 988 | + self, |
| 989 | + *, |
| 990 | + user_id: str, |
| 991 | + session_id: str, |
| 992 | + invocation_id: Optional[str] = None, |
| 993 | + new_message: Optional[types.Content] = None, |
| 994 | + state_delta: Optional[dict[str, Any]] = None, |
| 995 | + run_config: Optional[RunConfig] = None, |
| 996 | + ): |
| 997 | + del user_id, session_id, invocation_id, new_message, state_delta |
| 998 | + del run_config |
| 999 | + return _event_stream() |
| 1000 | + |
| 1001 | + async def close(self): |
| 1002 | + pass |
| 1003 | + |
| 1004 | + monkeypatch.setattr('google.adk.runners.Runner', StubRunner) |
| 1005 | + |
| 1006 | + tool_agent = Agent( |
| 1007 | + name='tool_agent', |
| 1008 | + model=testing_utils.MockModel.create(responses=['unused']), |
| 1009 | + ) |
| 1010 | + agent_tool = AgentTool(agent=tool_agent) |
| 1011 | + |
| 1012 | + session_service = InMemorySessionService() |
| 1013 | + session = await session_service.create_session( |
| 1014 | + app_name='test_app', user_id='test_user' |
| 1015 | + ) |
| 1016 | + |
| 1017 | + invocation_context = InvocationContext( |
| 1018 | + invocation_id='invocation_id', |
| 1019 | + agent=tool_agent, |
| 1020 | + session=session, |
| 1021 | + session_service=session_service, |
| 1022 | + ) |
| 1023 | + tool_context = ToolContext(invocation_context=invocation_context) |
| 1024 | + |
| 1025 | + tool_result = await agent_tool.run_async( |
| 1026 | + args={'request': 'test request'}, |
| 1027 | + tool_context=tool_context, |
| 1028 | + ) |
| 1029 | + |
| 1030 | + assert tool_result == 'IRR=0.1472' |
| 1031 | + |
| 1032 | + |
946 | 1033 | @mark.asyncio |
947 | 1034 | async def test_run_async_handles_none_parts_in_response(): |
948 | 1035 | """Verify run_async handles None parts in response without raising TypeError.""" |
|
0 commit comments