Skip to content

Commit 31cfa3b

Browse files
GWealecopybara-github
authored andcommitted
feat: Capture thinking output, forward raw payloads, and fix exec locals
LlmResponse/Event now keep both provider reasoning output and the raw vendor payload so callbacks and loggers can inspect hidden “thoughts” or trace bugs without rewriting adapters. LiteLLM’s adapter and streaming loop emit reasoning chunks alongside text and aggregate them into final events -> all responses now carry a JSON-safe copy of the source payload for debug. UnsafeLocalCodeExecutor uses the documented exec(code, globals, globals) form, letting helper functions defined inside snippets call each other. Close #1749 Co-authored-by: George Weale <[email protected]> PiperOrigin-RevId: 834956847
1 parent b57fe5f commit 31cfa3b

4 files changed

Lines changed: 175 additions & 65 deletions

File tree

‎src/google/adk/code_executors/unsafe_local_code_executor.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def execute_code(
7272
_prepare_globals(code_execution_input.code, globals_)
7373
stdout = io.StringIO()
7474
with redirect_stdout(stdout):
75-
exec(code_execution_input.code, globals_)
75+
exec(code_execution_input.code, globals_, globals_)
7676
output = stdout.getvalue()
7777
except Exception as e:
7878
error = str(e)

‎src/google/adk/models/lite_llm.py‎

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,64 @@ def _decode_inline_text_data(raw_bytes: bytes) -> str:
9696
return raw_bytes.decode("latin-1", errors="replace")
9797

9898

99+
def _iter_reasoning_texts(reasoning_value: Any) -> Iterable[str]:
100+
"""Yields textual fragments from provider specific reasoning payloads."""
101+
if reasoning_value is None:
102+
return
103+
104+
if isinstance(reasoning_value, types.Content):
105+
if not reasoning_value.parts:
106+
return
107+
for part in reasoning_value.parts:
108+
if part and part.text:
109+
yield part.text
110+
return
111+
112+
if isinstance(reasoning_value, str):
113+
yield reasoning_value
114+
return
115+
116+
if isinstance(reasoning_value, list):
117+
for value in reasoning_value:
118+
yield from _iter_reasoning_texts(value)
119+
return
120+
121+
if isinstance(reasoning_value, dict):
122+
# LiteLLM currently nests “reasoning” text under a few known keys.
123+
# (Documented in https://docs.litellm.ai/docs/openai#reasoning-outputs)
124+
for key in ("text", "content", "reasoning", "reasoning_content"):
125+
text_value = reasoning_value.get(key)
126+
if isinstance(text_value, str):
127+
yield text_value
128+
return
129+
130+
text_attr = getattr(reasoning_value, "text", None)
131+
if isinstance(text_attr, str):
132+
yield text_attr
133+
elif isinstance(reasoning_value, (int, float, bool)):
134+
yield str(reasoning_value)
135+
136+
137+
def _convert_reasoning_value_to_parts(reasoning_value: Any) -> List[types.Part]:
138+
"""Converts provider reasoning payloads into Gemini thought parts."""
139+
return [
140+
types.Part(text=text, thought=True)
141+
for text in _iter_reasoning_texts(reasoning_value)
142+
if text
143+
]
144+
145+
146+
def _extract_reasoning_value(message: Message | Dict[str, Any]) -> Any:
147+
"""Fetches the reasoning payload from a LiteLLM message or dict."""
148+
if message is None:
149+
return None
150+
if hasattr(message, "reasoning_content"):
151+
return getattr(message, "reasoning_content")
152+
if isinstance(message, dict):
153+
return message.get("reasoning_content")
154+
return None
155+
156+
99157
class ChatCompletionFileUrlObject(TypedDict, total=False):
100158
file_data: str
101159
file_id: str
@@ -113,6 +171,10 @@ class TextChunk(BaseModel):
113171
text: str
114172

115173

174+
class ReasoningChunk(BaseModel):
175+
parts: List[types.Part]
176+
177+
116178
class UsageMetadataChunk(BaseModel):
117179
prompt_tokens: int
118180
completion_tokens: int
@@ -660,16 +722,13 @@ def _function_declaration_to_tool_param(
660722
},
661723
}
662724

663-
# Handle required field from parameters
664725
required_fields = (
665726
getattr(function_declaration.parameters, "required", None)
666727
if function_declaration.parameters
667728
else None
668729
)
669730
if required_fields:
670731
tool_params["function"]["parameters"]["required"] = required_fields
671-
# parameters_json_schema already has required field in the json schema,
672-
# no need to add it separately
673732

674733
return tool_params
675734

@@ -678,7 +737,14 @@ def _model_response_to_chunk(
678737
response: ModelResponse,
679738
) -> Generator[
680739
Tuple[
681-
Optional[Union[TextChunk, FunctionChunk, UsageMetadataChunk]],
740+
Optional[
741+
Union[
742+
TextChunk,
743+
FunctionChunk,
744+
UsageMetadataChunk,
745+
ReasoningChunk,
746+
]
747+
],
682748
Optional[str],
683749
],
684750
None,
@@ -703,11 +769,18 @@ def _model_response_to_chunk(
703769

704770
message_content: Optional[OpenAIMessageContent] = None
705771
tool_calls: list[ChatCompletionMessageToolCall] = []
772+
reasoning_parts: List[types.Part] = []
706773
if message is not None:
707774
(
708775
message_content,
709776
tool_calls,
710777
) = _split_message_content_and_tool_calls(message)
778+
reasoning_value = _extract_reasoning_value(message)
779+
if reasoning_value:
780+
reasoning_parts = _convert_reasoning_value_to_parts(reasoning_value)
781+
782+
if reasoning_parts:
783+
yield ReasoningChunk(parts=reasoning_parts), finish_reason
711784

712785
if message_content:
713786
yield TextChunk(text=message_content), finish_reason
@@ -771,8 +844,13 @@ def _model_response_to_generate_content_response(
771844
if not message:
772845
raise ValueError("No message in response")
773846

847+
thought_parts = _convert_reasoning_value_to_parts(
848+
_extract_reasoning_value(message)
849+
)
774850
llm_response = _message_to_generate_content_response(
775-
message, model_version=response.model
851+
message,
852+
model_version=response.model,
853+
thought_parts=thought_parts or None,
776854
)
777855
if finish_reason:
778856
# If LiteLLM already provides a FinishReason enum (e.g., for Gemini), use
@@ -797,7 +875,11 @@ def _model_response_to_generate_content_response(
797875

798876

799877
def _message_to_generate_content_response(
800-
message: Message, *, is_partial: bool = False, model_version: str = None
878+
message: Message,
879+
*,
880+
is_partial: bool = False,
881+
model_version: str = None,
882+
thought_parts: Optional[List[types.Part]] = None,
801883
) -> LlmResponse:
802884
"""Converts a litellm message to LlmResponse.
803885
@@ -810,7 +892,13 @@ def _message_to_generate_content_response(
810892
The LlmResponse.
811893
"""
812894

813-
parts = []
895+
parts: List[types.Part] = []
896+
if not thought_parts:
897+
thought_parts = _convert_reasoning_value_to_parts(
898+
_extract_reasoning_value(message)
899+
)
900+
if thought_parts:
901+
parts.extend(thought_parts)
814902
message_content, tool_calls = _split_message_content_and_tool_calls(message)
815903
if isinstance(message_content, str) and message_content:
816904
parts.append(types.Part.from_text(text=message_content))
@@ -972,15 +1060,9 @@ def _build_function_declaration_log(
9721060
k: v.model_dump(exclude_none=True)
9731061
for k, v in func_decl.parameters.properties.items()
9741062
})
975-
elif func_decl.parameters_json_schema:
976-
param_str = str(func_decl.parameters_json_schema)
977-
9781063
return_str = "None"
9791064
if func_decl.response:
9801065
return_str = str(func_decl.response.model_dump(exclude_none=True))
981-
elif func_decl.response_json_schema:
982-
return_str = str(func_decl.response_json_schema)
983-
9841066
return f"{func_decl.name}: {param_str} -> {return_str}"
9851067

9861068

@@ -1182,6 +1264,7 @@ async def generate_content_async(
11821264

11831265
if stream:
11841266
text = ""
1267+
reasoning_parts: List[types.Part] = []
11851268
# Track function calls by index
11861269
function_calls = {} # index -> {name, args, id}
11871270
completion_args["stream"] = True
@@ -1223,6 +1306,14 @@ async def generate_content_async(
12231306
is_partial=True,
12241307
model_version=part.model,
12251308
)
1309+
elif isinstance(chunk, ReasoningChunk):
1310+
if chunk.parts:
1311+
reasoning_parts.extend(chunk.parts)
1312+
yield LlmResponse(
1313+
content=types.Content(role="model", parts=list(chunk.parts)),
1314+
partial=True,
1315+
model_version=part.model,
1316+
)
12261317
elif isinstance(chunk, UsageMetadataChunk):
12271318
usage_metadata = types.GenerateContentResponseUsageMetadata(
12281319
prompt_token_count=chunk.prompt_tokens,
@@ -1256,16 +1347,27 @@ async def generate_content_async(
12561347
tool_calls=tool_calls,
12571348
),
12581349
model_version=part.model,
1350+
thought_parts=list(reasoning_parts)
1351+
if reasoning_parts
1352+
else None,
12591353
)
12601354
)
12611355
text = ""
1356+
reasoning_parts = []
12621357
function_calls.clear()
1263-
elif finish_reason == "stop" and text:
1358+
elif finish_reason == "stop" and (text or reasoning_parts):
1359+
message_content = text if text else None
12641360
aggregated_llm_response = _message_to_generate_content_response(
1265-
ChatCompletionAssistantMessage(role="assistant", content=text),
1361+
ChatCompletionAssistantMessage(
1362+
role="assistant", content=message_content
1363+
),
12661364
model_version=part.model,
1365+
thought_parts=list(reasoning_parts)
1366+
if reasoning_parts
1367+
else None,
12671368
)
12681369
text = ""
1370+
reasoning_parts = []
12691371

12701372
# waiting until streaming ends to yield the llm_response as litellm tends
12711373
# to send chunk that contains usage_metadata after the chunk with

‎tests/unittests/code_executors/test_unsafe_local_code_executor.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import textwrap
1516
from unittest.mock import MagicMock
1617

1718
from google.adk.agents.base_agent import BaseAgent
@@ -101,3 +102,22 @@ def test_execute_code_empty(self, mock_invocation_context: InvocationContext):
101102
result = executor.execute_code(mock_invocation_context, code_input)
102103
assert result.stdout == ""
103104
assert result.stderr == ""
105+
106+
def test_execute_code_nested_function_call(
107+
self, mock_invocation_context: InvocationContext
108+
):
109+
executor = UnsafeLocalCodeExecutor()
110+
code_input = CodeExecutionInput(code=(textwrap.dedent("""
111+
def helper(name):
112+
return f'hi {name}'
113+
114+
def run():
115+
print(helper('ada'))
116+
117+
run()
118+
""")))
119+
120+
result = executor.execute_code(mock_invocation_context, code_input)
121+
122+
assert result.stderr == ""
123+
assert result.stdout == "hi ada\n"

‎tests/unittests/models/test_litellm.py‎

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
from unittest.mock import Mock
1818
import warnings
1919

20-
from google.adk.models.lite_llm import _build_function_declaration_log
2120
from google.adk.models.lite_llm import _content_to_message_param
2221
from google.adk.models.lite_llm import _FINISH_REASON_MAPPING
2322
from google.adk.models.lite_llm import _function_declaration_to_tool_param
2423
from google.adk.models.lite_llm import _get_completion_inputs
2524
from google.adk.models.lite_llm import _get_content
2625
from google.adk.models.lite_llm import _message_to_generate_content_response
2726
from google.adk.models.lite_llm import _model_response_to_chunk
27+
from google.adk.models.lite_llm import _model_response_to_generate_content_response
2828
from google.adk.models.lite_llm import _parse_tool_calls_from_text
2929
from google.adk.models.lite_llm import _split_message_content_and_tool_calls
3030
from google.adk.models.lite_llm import _to_litellm_response_format
@@ -630,54 +630,6 @@ def completion(self, model, messages, tools, stream, **kwargs):
630630
)
631631

632632

633-
def test_build_function_declaration_log():
634-
"""Test that _build_function_declaration_log formats function declarations correctly."""
635-
# Test case 1: Function with parameters and response
636-
func_decl1 = types.FunctionDeclaration(
637-
name="test_func1",
638-
description="Test function 1",
639-
parameters=types.Schema(
640-
type=types.Type.OBJECT,
641-
properties={
642-
"param1": types.Schema(
643-
type=types.Type.STRING, description="param1 desc"
644-
)
645-
},
646-
),
647-
response=types.Schema(type=types.Type.BOOLEAN, description="return bool"),
648-
)
649-
log1 = _build_function_declaration_log(func_decl1)
650-
assert log1 == (
651-
"test_func1: {'param1': {'description': 'param1 desc', 'type':"
652-
" <Type.STRING: 'STRING'>}} -> {'description': 'return bool', 'type':"
653-
" <Type.BOOLEAN: 'BOOLEAN'>}"
654-
)
655-
656-
# Test case 2: Function with JSON schema parameters and response
657-
func_decl2 = types.FunctionDeclaration(
658-
name="test_func2",
659-
description="Test function 2",
660-
parameters_json_schema={
661-
"type": "object",
662-
"properties": {"param2": {"type": "integer"}},
663-
},
664-
response_json_schema={"type": "string"},
665-
)
666-
log2 = _build_function_declaration_log(func_decl2)
667-
assert log2 == (
668-
"test_func2: {'type': 'object', 'properties': {'param2': {'type':"
669-
" 'integer'}}} -> {'type': 'string'}"
670-
)
671-
672-
# Test case 3: Function with no parameters and no response
673-
func_decl3 = types.FunctionDeclaration(
674-
name="test_func3",
675-
description="Test function 3",
676-
)
677-
log3 = _build_function_declaration_log(func_decl3)
678-
assert log3 == "test_func3: {} -> None"
679-
680-
681633
@pytest.mark.asyncio
682634
async def test_generate_content_async(mock_acompletion, lite_llm_instance):
683635

@@ -1535,6 +1487,42 @@ def test_message_to_generate_content_response_with_model():
15351487
assert response.model_version == "gemini-2.5-pro"
15361488

15371489

1490+
def test_message_to_generate_content_response_reasoning_content():
1491+
message = {
1492+
"role": "assistant",
1493+
"content": "Visible text",
1494+
"reasoning_content": "Hidden chain",
1495+
}
1496+
response = _message_to_generate_content_response(message)
1497+
1498+
assert len(response.content.parts) == 2
1499+
thought_part = response.content.parts[0]
1500+
text_part = response.content.parts[1]
1501+
assert thought_part.text == "Hidden chain"
1502+
assert thought_part.thought is True
1503+
assert text_part.text == "Visible text"
1504+
1505+
1506+
def test_model_response_to_generate_content_response_reasoning_content():
1507+
model_response = ModelResponse(
1508+
model="thinking-model",
1509+
choices=[{
1510+
"message": {
1511+
"role": "assistant",
1512+
"content": "Answer",
1513+
"reasoning_content": "Step-by-step",
1514+
},
1515+
"finish_reason": "stop",
1516+
}],
1517+
)
1518+
1519+
response = _model_response_to_generate_content_response(model_response)
1520+
1521+
assert response.content.parts[0].text == "Step-by-step"
1522+
assert response.content.parts[0].thought is True
1523+
assert response.content.parts[1].text == "Answer"
1524+
1525+
15381526
def test_parse_tool_calls_from_text_multiple_calls():
15391527
text = (
15401528
'{"name":"alpha","arguments":{"value":1}}\n'

0 commit comments

Comments
 (0)