Skip to content

Commit f022307

Browse files
Adeev Mardiawyf7107
authored andcommitted
fix: avoid UserWarning in _build_response_log when response has funct…
Merge google#6127 # Fix: `_build_response_log()` triggers genai SDK UserWarning on every tool call Closes google#4685 ## What's the problem? Every time an ADK agent invokes a tool, the debug logger calls `_build_response_log(response)`, which includes `resp.text` in its f-string: ```python return f""" LLM Response: ... Text: {resp.text} # ← triggers UserWarning ... """ ``` The `GenerateContentResponse.text` property in the google-genai SDK raises a `UserWarning` whenever the response contains non-text parts — which is exactly the case when the model responds with a `function_call`. This means **every single tool invocation floods the log with warnings** like: ``` UserWarning: Warning: there are non-text parts in the response: ['function_call'],returning concatenated text result from text parts, check `response.parts` directly to inspect non-text parts warnings.warn( ``` Since `_build_response_log` is only called inside `if logger.isEnabledFor(logging.DEBUG)`, this hits any developer who enables debug logging — which is common when debugging agents. ## Root cause `GenerateContentResponse.text` is a convenience property that warns when mixed content is present. Accessing it in a log formatter silently poisons the log output whenever agents use tools. ## Fix Replace `resp.text` with a manual join of only the text parts from `resp.candidates`, bypassing the warning entirely: ```python # Before return f""" ... Text: {resp.text} ... """ # After — safe extraction with no warning text_parts = [] if resp.candidates: for candidate in resp.candidates: if candidate.content and candidate.content.parts: text_parts.extend( p.text for p in candidate.content.parts if p.text is not None ) text = ''.join(text_parts) return f""" ... Text: {text} ... """ ``` This produces identical output when only text parts are present, and correctly shows an empty string (rather than a warning) when the response is a function call — which is the right behavior for a debug log. ## Files changed - `src/google/adk/models/google_llm.py` — 10-line change inside `_build_response_log()`, no other logic touched Co-authored-by: Yifan Wang <wanyif@google.com> COPYBARA_INTEGRATE_REVIEW=google#6127 from AdeevMardia2008:fix/response-log-text-warning 011c153 PiperOrigin-RevId: 933440866
1 parent d9f189c commit f022307

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/google/adk/models/google_llm.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,30 @@ def _build_response_log(resp: types.GenerateContentResponse) -> str:
614614
function_calls_text.append(
615615
f'name: {func_call.name}, args: {func_call.args}'
616616
)
617+
# Avoid accessing resp.text directly: the genai SDK raises a UserWarning
618+
# whenever .text is accessed on a response that contains non-text parts
619+
# (e.g. function_call). This floods logs on every tool invocation.
620+
# Instead, manually join only the text parts from candidates.
621+
text_parts = []
622+
# Mimic resp.text behavior exactly but without triggering linter warnings:
623+
# 1. Only use the first candidate.
624+
# 2. Exclude thought/reasoning parts.
625+
if (
626+
resp.candidates
627+
and resp.candidates[0].content
628+
and resp.candidates[0].content.parts
629+
):
630+
for part in resp.candidates[0].content.parts:
631+
if isinstance(part.text, str):
632+
if getattr(part, 'thought', False):
633+
continue
634+
text_parts.append(part.text)
635+
text = ''.join(text_parts)
617636
return f"""
618637
LLM Response:
619638
-----------------------------------------------------------
620639
Text:
621-
{resp.text}
640+
{text}
622641
-----------------------------------------------------------
623642
Function calls:
624643
{_NEW_LINE.join(function_calls_text)}

0 commit comments

Comments
 (0)