Skip to content

Commit b4554c9

Browse files
committed
refactor: extract streaming mode detection to _is_sse_streaming helper
Removes duplicated logic in _retry_same_model and _try_fallbacks (DRY).
1 parent 2078ed5 commit b4554c9

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

src/google/adk_community/plugins/llm_resilience_plugin.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -225,25 +225,33 @@ def _get_invocation_context(
225225
)
226226
return ic
227227

228-
async def _retry_same_model(
229-
self,
230-
*,
231-
callback_context: CallbackContext | InvocationContext,
232-
llm_request: LlmRequest,
233-
) -> Optional[LlmResponse]:
234-
invocation_context = self._get_invocation_context(callback_context)
235-
# Determine streaming mode
228+
def _is_sse_streaming(self, invocation_context: InvocationContext) -> bool:
229+
"""Check if SSE streaming mode is enabled.
230+
231+
Args:
232+
invocation_context: The invocation context to check.
233+
234+
Returns:
235+
True if SSE streaming is enabled, False otherwise.
236+
"""
236237
streaming_mode = getattr(
237238
invocation_context.run_config, "streaming_mode", None
238239
)
239-
stream = False
240240
try:
241-
# Only SSE streaming is supported in generate_content_async
242241
from google.adk.agents.run_config import StreamingMode
243242

244-
stream = streaming_mode == StreamingMode.SSE
243+
return streaming_mode == StreamingMode.SSE
245244
except (ImportError, AttributeError):
246-
pass
245+
return False
246+
247+
async def _retry_same_model(
248+
self,
249+
*,
250+
callback_context: CallbackContext | InvocationContext,
251+
llm_request: LlmRequest,
252+
) -> Optional[LlmResponse]:
253+
invocation_context = self._get_invocation_context(callback_context)
254+
stream = self._is_sse_streaming(invocation_context)
247255

248256
agent = invocation_context.agent
249257
llm = agent.canonical_model
@@ -283,17 +291,7 @@ async def _try_fallbacks(
283291
llm_request: LlmRequest,
284292
) -> Optional[LlmResponse]:
285293
invocation_context = self._get_invocation_context(callback_context)
286-
# Determine streaming mode
287-
streaming_mode = getattr(
288-
invocation_context.run_config, "streaming_mode", None
289-
)
290-
stream = False
291-
try:
292-
from google.adk.agents.run_config import StreamingMode
293-
294-
stream = streaming_mode == StreamingMode.SSE
295-
except (ImportError, AttributeError):
296-
pass
294+
stream = self._is_sse_streaming(invocation_context)
297295

298296
for model_name in self.fallback_models:
299297
try:

0 commit comments

Comments
 (0)