AgentEvaluator with num_runs > 1 reuses the pinned session across runs, so later runs score wrong

Aperta
#8,351 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Start in AgentEvaluator._get_eval_results_by_eval_id, which sends one InferenceRequest per run with InferenceConfig.num_runs=1, then read LocalEvalService.perform_inference and _without_pinned_session_id, which only drops a pinned session when a single request asks for several runs. Done means a regression test in tests/unittests/evaluation/test_agent_evaluator.py where a pinned session_id with num_runs=3 gives each run a fresh session, so every run passes with users=1 counter=0.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

AgentEvaluator.evaluate / evaluate_eval_set with num_runs > 1 gives wrong scores for any eval case whose session_input pins a session_id. The AgentEvaluator docstring suggests pinning a session id to preload artifacts, so this is a normal setup.

Run 2 and later continue the session that run 1 left behind. The agent sees the earlier run's conversation and changed state, and session_input.state is not applied again. Later runs then pass or fail because of leftover context, not because of the agent's behaviour, so pass rates and pass@k-style results are wrong. adk eval --num_runs on the same eval set gives correct results.

Reproduction

Run on current main (3e322e9a). The agent replies with the number of user messages in its session and a state counter that session_input.state seeds to 0. Each run should therefore reply users=1 counter=0.

import asyncio
from google.adk.agents.base_agent import BaseAgent
from google.adk.events.event import Event, EventActions
from google.adk.evaluation.agent_evaluator import AgentEvaluator
from google.adk.evaluation.eval_case import EvalCase, Invocation, SessionInput
from google.adk.evaluation.eval_metrics import EvalMetric
from google.adk.evaluation.eval_set import EvalSet
from google.adk.evaluation.simulation.user_simulator_provider import UserSimulatorProvider
from google.genai import types

class Probe(BaseAgent):
  async def _run_async_impl(self, ctx):
    n_user = sum(1 for e in ctx.session.events if e.author == "user")
    counter = ctx.session.state.get("counter")
    yield Event(author=self.name, invocation_id=ctx.invocation_id,
                content=types.Content(role="model", parts=[types.Part(text=f"users={n_user} counter={counter}")]),
                actions=EventActions(state_delta={"counter": (counter or 0) + 1}))

def case(session_id):
  return EvalCase(
      eval_id="c",
      conversation=[Invocation(user_content=types.Content(role="user", parts=[types.Part(text="hi")]),
                               final_response=types.Content(role="model", parts=[types.Part(text="users=1 counter=0")]))],
      session_input=SessionInput(app_name="app", user_id="u", session_id=session_id, state={"counter": 0}))

async def run(session_id):
  res = await AgentEvaluator._get_eval_results_by_eval_id(
      agent_for_eval=Probe(name="probe"), eval_set=EvalSet(eval_set_id="s", eval_cases=[case(session_id)]),
      eval_metrics=[EvalMetric(metric_name="response_match_score", threshold=1.0)],
      num_runs=3, user_simulator_provider=UserSimulatorProvider(), app_name="app")
  for r in sorted(res["c"], key=lambda r: r.repetition_index):
    resp = r.eval_metric_result_per_invocation[0].actual_invocation.final_response.parts[0].text
    print(f"  rep={r.repetition_index} session={r.session_id} response={resp!r} {r.final_eval_status.name}")

for sid in ["pinned", None]:
  print(f"session_id={sid!r}:")
  asyncio.run(run(sid))

(The public AgentEvaluator.evaluate_eval_set(..., num_runs=3) fails the same way with an AssertionError. The private helper is used here to print each run.)

Actual

session_id='pinned':
  rep=0 session=pinned response='users=1 counter=0' PASSED
  rep=1 session=pinned response='users=2 counter=1' FAILED
  rep=2 session=pinned response='users=3 counter=2' FAILED
session_id=None:
  rep=0 session=adk-eval-session-... response='users=1 counter=0' PASSED
  rep=1 session=adk-eval-session-... response='users=1 counter=0' PASSED
  rep=2 session=adk-eval-session-... response='users=1 counter=0' PASSED

Expected

With the pinned id, every run replies users=1 counter=0 and passes, as without the pin. The InferenceConfig.num_runs docstring (base_eval_service.py) says a pinned session id is ignored when num_runs > 1.

Cause

AgentEvaluator._get_eval_results_by_eval_id sends num_runs separate InferenceRequests, each with InferenceConfig.num_runs=1. LocalEvalService.perform_inference drops a pinned session id (_without_pinned_session_id) only when a single request asks for more than one run, so on this path the pin is never dropped. Every run uses the same session in the shared in-memory session service.

Proposed fix

Send one InferenceRequest with inference_config.num_runs = num_runs, which is what adk eval does. The service then gives each run a fresh session and sets repetition_index itself. As in adk eval, repeated runs now share the parallelism pool instead of running one after another. One regression test is in tests/unittests/evaluation/test_agent_evaluator.py.

I used an AI coding assistant (Claude) to find and analyse this; I reproduced it and checked the fix myself.

I've tested a fix with a regression test: https://github.com/chaitanya0305/adk-python/compare/main...chaitanya0305:adk-python:fix/agent-evaluator-pinned-session-num-runs. If this is accepted, I'll open the PR straight away.

Lingua principale
Python
Stelle
22.3k
Fork
4.4k
Merge medio
1g 2h
PR unite (30g)
6

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/adk-python

Tutte le issue di google/adk-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.