Repository navigation
fix(trace): read final_response from the current invocation and parse the plugin's parts - #601
Conversation
… the plugin's parts Address the follow-ups in GoogleCloudPlatform#595: - Read only the session's latest invocation. Nested invocations are part of the run but never its answer. - Parse the ADK plugin's part encoding structurally, in O(n log n): a turn that calls a tool holds no answer wherever the call is, clipped text keeps its visible text, and a call needs a function name. - Apply the encoding only to plugin rows; bundled-writer rows and AGENT_COMPLETED rows without attributes.adk are verbatim. - Read AGENT_RESPONSE rows, preferring their text_summary. - Always return a string or None; skip empty and usage-only turns in SessionTrace; read the loader's {"raw": ...} wrapper. - Make _flag treat NaN, NaT and bytes correctly. - Pick the SQL judge's final answer with the same rules, and drop the unused final_response column from the insights transcript query. Fixes GoogleCloudPlatform#595.
fbb7a26 to
ff0ebe7
Compare
caohy1988
left a comment
There was a problem hiding this comment.
Reviewed at ff0ebe7. Putting both accessors and the judge SQL behind one selection path is a nice cleanup, reading AGENT_RESPONSE fixes sessions whose only answer lives there, and the final-response tests plus the evaluator and insights suites pass here.
Approving. I filed #603 for edge cases I could reproduce offline. The most important ones: a new user message whose fresh invocation id hasn't repeated yet can still return the previous turn's answer, nested invocations deeper than one level make SQL drop the parent's answer, a JSON-string answer can lose fields in the row loader, and native content objects or part lists (including a function-call-only content) reach the judge as serialized JSON. A few Python/SQL parity gaps are listed there too.
LEFT() is reserved in SQLite, so the CI parity tests failed to parse the query. SUBSTR(..., 1, 1) is the same in BigQuery.
Summary
Fixes #595: the follow-ups from reviewing #593.
Trace.final_responseandSessionTrace.extract_final_response()now share one implementation,trace._final_answer. The SQL judge picks the final answer with the same rules. Because the rules in #594 readTrace.final_response, this also covers theAGENT_RESPONSEitem of #600.What changed, by item
1. Only the current invocation is read.
attributes.adk, so theirinvocation_idalways marks the run, even after an event-type filter leaves one row of it.seed_events, for example, gives every row its own id. There, the run is the latest id that is shared or that sits on a response row. Otherwise the run is the rows after the lastUSER_MESSAGE_RECEIVED.TraceEventgains aninvocation_idfield, appended last.2. The plugin's encoding is applied only to its rows.
attributes.writer) are verbatim.AGENT_COMPLETEDrows withoutattributes.adk. The plugin never fills these; EvalBench imports do.text: '...'wrapper, as on main.call: <x>counts as a call only when<x>is a function name.call: 555-0100is prose;call: 1password_lookupis a call.text_summaryis never decoded.3. Clipped text is not read as a tool call.
...[TRUNCATED]marker....'ERROR' | c...[TRUNCATED]) stays text, because the cut may as well have landed inside the text.4. Always a string or
None.text_summary, or else compact JSON.call: exit_loopandNoneare not answers.5.
_flagtreats NaN, NaT,pd.NAandb"false"as false.6. The
is_truncateddocs now describe it as the producer's row-level flag.7. Turns are parsed part by part.
call: x | other, a call followed by text, trailing whitespace, and calls with arguments.other,resp:, code only) are skipped.|may be text. A text or code part ends at the first separator after which the rest of the turn still parses.8. Pre-existing gaps.
extract_final_responsenow skips empty and usage-only turns, and unwrapstext: '...'likeTracedoes.{"raw": ...}wrapper is read.AGENT_RESPONSErows are read as model turns. The plugin writes them for a final response (its text without thoughts, or the arguments of afinal_response_tool_namestool), and BQCA compatibility views answer there. A non-blanktext_summaryis read first, as the plugin's own eval views do.PerformanceEvaluatornow fetchesAGENT_RESPONSEby default.AI.GENERATEandML.GENERATE_TEXT) gets the answer from shared CTEs,trace._final_response_sql, that apply these rules. The insights transcript query drops itsfinal_responsecolumn, which no caller read.Behaviour changes to note
SessionTrace.extract_final_response()now returnsDone.where it returnedtext: 'Done.'. On our 933 KramaBench runs, this is the only change: 686 sessions.Trace.final_responseis identical on all 933.a' | text: 'b.Limits
x' | call: f | text: 'y, reads as those parts. Nothing in the encoding tells the two apart.' |followed by a part prefix, or on a code part followed by a clipped call. On generated producer-shaped sessions, that was 0–2 in 4,000.Testing
tests/test_final_response.py(90 tests) runs every case through both accessors and checks that they agree.tests/test_final_response_sql.py(156 tests) runs the judge SQL in SQLite, with BigQuery's functions shimmed in Python, and checks that it returns whatTrace.final_responsereturns. Cases include generated plugin-shaped turns with code, tables and clipping._format_contentdoes. On clean, unclipped text this PR is always right. It fixes about 10,500 turns that fix(trace): skip tool-call and empty turns in final_response; keep Span.is_truncated #593 got wrong. fix(trace): skip tool-call and empty turns in final_response; keep Span.is_truncated #593 got 51 right that this PR gets wrong; all are cuts right after a text part, the trade-off in item 3.pyteston Python 3.13 (macOS): 11619 passed, 87 skipped. I deselected the 4browser_smokeLooker Studio tests because they hang on macOS.bash autoformat.shleaves no diff.agent_eventstable, with JSONcontentandattributes.AI.GENERATEjudge query: valid.ML.GENERATE_TEXTtemplate fails with "Table-valued function is not expected here: ML.GENERATE_TEXT". The template on main (1e93119) fails the same way, so the cause is pre-existing: the template calls the TVF as a scalar. The error is in the outerSELECT, after the CTEs, so it is out of scope here.