Skip to content

Commit 696852a

Browse files
ankursharmascopybara-github
authored andcommitted
chore: Add default retry options as fall back to llm_request that are made during evals
In order to make evals more resilient to temporary model failures, we add retry options to llm_requests that are made during evals. Note that this is a fall back option, if the developer has already specified their own retry options, then those will be honored. Co-authored-by: Ankur Sharma <ankusharma@google.com> PiperOrigin-RevId: 832383755
1 parent 9b75456 commit 696852a

File tree

6 files changed

+168
-1
lines changed

6 files changed

+168
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from typing import Optional
18+
19+
from google.genai import types
20+
from typing_extensions import override
21+
22+
from ..agents.callback_context import CallbackContext
23+
from ..models.llm_request import LlmRequest
24+
from ..models.llm_response import LlmResponse
25+
from ..plugins.base_plugin import BasePlugin
26+
27+
_RETRY_HTTP_STATUS_CODES = (
28+
408, # Request timeout.
29+
429, # Too many requests.
30+
500, # Internal server error.
31+
502, # Bad gateway.
32+
503, # Service unavailable.
33+
504, # Gateway timeout
34+
)
35+
_DEFAULT_HTTP_RETRY_OPTIONS = types.HttpRetryOptions(
36+
attempts=7,
37+
initial_delay=5.0,
38+
max_delay=120,
39+
exp_base=2.0,
40+
http_status_codes=_RETRY_HTTP_STATUS_CODES,
41+
)
42+
43+
44+
def add_default_retry_options_if_not_present(llm_request: LlmRequest):
45+
"""Adds default HTTP Retry Options, if they are not present on the llm_request.
46+
47+
NOTE: This implementation is intended for eval systems internal usage. Do not
48+
take direct dependency on it.
49+
"""
50+
llm_request.config = llm_request.config or types.GenerateContentConfig()
51+
52+
llm_request.config.http_options = (
53+
llm_request.config.http_options or types.HttpOptions()
54+
)
55+
llm_request.config.http_options.retry_options = (
56+
llm_request.config.http_options.retry_options
57+
or _DEFAULT_HTTP_RETRY_OPTIONS
58+
)
59+
60+
61+
class EnsureRetryOptionsPlugin(BasePlugin):
62+
"""This plugin adds retry options to llm_request, if they are not present.
63+
64+
This is done to ensure that temporary outages with the model provider don't
65+
affect eval runs.
66+
67+
NOTE: This implementation is intended for eval systems internal usage. Do not
68+
take direct dependency on it.
69+
"""
70+
71+
@override
72+
async def before_model_callback(
73+
self, *, callback_context: CallbackContext, llm_request: LlmRequest
74+
) -> Optional[LlmResponse]:
75+
add_default_retry_options_if_not_present(llm_request)

src/google/adk/evaluation/evaluation_generator.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from ..sessions.in_memory_session_service import InMemorySessionService
3636
from ..sessions.session import Session
3737
from ..utils.context_utils import Aclosing
38+
from ._retry_options_utils import EnsureRetryOptionsPlugin
3839
from .app_details import AgentDetails
3940
from .app_details import AppDetails
4041
from .eval_case import EvalCase
@@ -225,13 +226,19 @@ async def _generate_inferences_from_root_agent(
225226
request_intercepter_plugin = _RequestIntercepterPlugin(
226227
name="request_intercepter_plugin"
227228
)
229+
# We ensure that there is some kind of retries on the llm_requests that are
230+
# generated from the Agent. This is done to make inferencing step of evals
231+
# more resilient to temporary model failures.
232+
ensure_retry_options_plugin = EnsureRetryOptionsPlugin(
233+
name="ensure_retry_options"
234+
)
228235
async with Runner(
229236
app_name=app_name,
230237
agent=root_agent,
231238
artifact_service=artifact_service,
232239
session_service=session_service,
233240
memory_service=memory_service,
234-
plugins=[request_intercepter_plugin],
241+
plugins=[request_intercepter_plugin, ensure_retry_options_plugin],
235242
) as runner:
236243
events = []
237244
while True:

src/google/adk/evaluation/hallucinations_v1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from ..models.registry import LLMRegistry
3333
from ..utils.context_utils import Aclosing
3434
from ..utils.feature_decorator import experimental
35+
from ._retry_options_utils import add_default_retry_options_if_not_present
3536
from .app_details import AppDetails
3637
from .eval_case import Invocation
3738
from .eval_case import InvocationEvent
@@ -526,6 +527,7 @@ async def _evaluate_nl_response(
526527
],
527528
config=self._model_config,
528529
)
530+
add_default_retry_options_if_not_present(segmenter_llm_request)
529531
try:
530532
async with Aclosing(
531533
self._judge_model.generate_content_async(segmenter_llm_request)
@@ -559,6 +561,7 @@ async def _evaluate_nl_response(
559561
],
560562
config=self._model_config,
561563
)
564+
add_default_retry_options_if_not_present(validator_llm_request)
562565
try:
563566
async with Aclosing(
564567
self._judge_model.generate_content_async(validator_llm_request)

src/google/adk/evaluation/llm_as_judge.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ..models.registry import LLMRegistry
2828
from ..utils.context_utils import Aclosing
2929
from ..utils.feature_decorator import experimental
30+
from ._retry_options_utils import add_default_retry_options_if_not_present
3031
from .common import EvalBaseModel
3132
from .eval_case import Invocation
3233
from .eval_metrics import BaseCriterion
@@ -142,6 +143,7 @@ async def evaluate_invocations(
142143
],
143144
config=self._judge_model_options.judge_model_config,
144145
)
146+
add_default_retry_options_if_not_present(llm_request)
145147
num_samples = self._judge_model_options.num_samples
146148
invocation_result_samples = []
147149
for _ in range(num_samples):

src/google/adk/evaluation/llm_backed_user_simulator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ..models.registry import LLMRegistry
2828
from ..utils.context_utils import Aclosing
2929
from ..utils.feature_decorator import experimental
30+
from ._retry_options_utils import add_default_retry_options_if_not_present
3031
from .conversation_scenarios import ConversationScenario
3132
from .evaluator import Evaluator
3233
from .user_simulator import BaseUserSimulatorConfig
@@ -200,6 +201,7 @@ async def _get_llm_response(
200201
),
201202
],
202203
)
204+
add_default_retry_options_if_not_present(llm_request)
203205

204206
response = ""
205207
async with Aclosing(self._llm.generate_content_async(llm_request)) as agen:
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.adk.agents.callback_context import CallbackContext
16+
from google.adk.evaluation import _retry_options_utils
17+
from google.adk.models.llm_request import LlmRequest
18+
from google.genai import types
19+
import pytest
20+
21+
22+
def test_add_retry_options_with_default_request():
23+
request = LlmRequest()
24+
_retry_options_utils.add_default_retry_options_if_not_present(request)
25+
assert request.config.http_options is not None
26+
assert (
27+
request.config.http_options.retry_options
28+
== _retry_options_utils._DEFAULT_HTTP_RETRY_OPTIONS
29+
)
30+
31+
32+
def test_add_retry_options_when_retry_options_is_none():
33+
request = LlmRequest()
34+
request.config.http_options = types.HttpOptions(retry_options=None)
35+
_retry_options_utils.add_default_retry_options_if_not_present(request)
36+
assert (
37+
request.config.http_options.retry_options
38+
== _retry_options_utils._DEFAULT_HTTP_RETRY_OPTIONS
39+
)
40+
41+
42+
def test_add_retry_options_does_not_override_existing_options():
43+
my_retry_options = types.HttpRetryOptions(attempts=1)
44+
request = LlmRequest()
45+
request.config.http_options = types.HttpOptions(
46+
retry_options=my_retry_options
47+
)
48+
_retry_options_utils.add_default_retry_options_if_not_present(request)
49+
assert request.config.http_options.retry_options == my_retry_options
50+
51+
52+
def test_add_retry_options_when_config_is_none():
53+
request = LlmRequest()
54+
request.config = None
55+
_retry_options_utils.add_default_retry_options_if_not_present(request)
56+
assert request.config is not None
57+
assert request.config.http_options is not None
58+
assert (
59+
request.config.http_options.retry_options
60+
== _retry_options_utils._DEFAULT_HTTP_RETRY_OPTIONS
61+
)
62+
63+
64+
@pytest.mark.asyncio
65+
async def test_ensure_retry_options_plugin(mocker):
66+
request = LlmRequest()
67+
plugin = _retry_options_utils.EnsureRetryOptionsPlugin(name="test_plugin")
68+
mock_invocation_context = mocker.MagicMock()
69+
mock_invocation_context.session.state = {}
70+
callback_context = CallbackContext(mock_invocation_context)
71+
await plugin.before_model_callback(
72+
callback_context=callback_context, llm_request=request
73+
)
74+
assert request.config.http_options is not None
75+
assert (
76+
request.config.http_options.retry_options
77+
== _retry_options_utils._DEFAULT_HTTP_RETRY_OPTIONS
78+
)

0 commit comments

Comments
 (0)