Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion core/src/agents/llm_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1498,13 +1498,17 @@ export class LlmAgent extends BaseAgent {
): AsyncGenerator<Event, void, void> {
while (true) {
let lastEvent: Event|undefined = undefined;
let hasFunctionResponse = false;
for await (const event of this.runOneStepAsync(context)) {
lastEvent = event;
if (getFunctionResponses(event).length > 0) {
hasFunctionResponse = true;
}
this.maybeSaveOutputToState(event);
yield event;
}

if (!lastEvent || isFinalResponse(lastEvent)) {
if (!lastEvent || (isFinalResponse(lastEvent) && !hasFunctionResponse)) {
Comment on lines 1499 to +1511
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be wrong as it will not break when there is final response but there is no any function response. It can lead to infinity loop as we are using unsafe while (true) practice.

I think we might need to find out another solution for that problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review ,based on my reading of the code, it looks like the loop does break when there is a final response and no function response. In that case, isFinalResponse(lastEvent) is true and hasFunctionResponse remains false, so the condition evaluates to true && true and exits the loop.

Basically this:

while (true) {
  let hasFunctionResponse = false;  // resets each iteration
  
  for await (const event of this.runOneStepAsync(context)) {
    if (getFunctionResponses(event).length > 0) {
      hasFunctionResponse = true;
    }
  }
  
  if (!lastEvent || (isFinalResponse(lastEvent) && !hasFunctionResponse)) {
    break;  // line 1512
  }
}

The hasFunctionResponse flag resets to false at the start of each iteration. As we process events from runOneStepAsync, if we encounter any function responses, we set it to true. After processing all events, we check whether we received a final response without any function responses. If so, we break. The only case where we continue looping is when hasFunctionResponse is true, meaning we just processed a tool response and need to send it back to the model for another turn.

Note the while(true) pattern was already present in the original code:

while (true) {
  // ...
  if (!lastEvent || isFinalResponse(lastEvent)) {
    break;
  }
}

I only modified the break condition, not the loop structure .

break;
}
if (lastEvent.partial) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/code_executors/built_in_code_executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {GenerateContentConfig} from '@google/genai'

import {InvocationContext} from '../agents/invocation_context.js';
import {LlmRequest} from '../models/llm_request.js';
import {isGemini2Model} from '../utils/model_name.js';
import {isGemini2OrLaterModel} from '../utils/model_name.js';

import {BaseCodeExecutor, ExecuteCodeParams} from './base_code_executor.js';
import {CodeExecutionInput, CodeExecutionResult} from './code_execution_utils.js';
Expand All @@ -28,7 +28,7 @@ export class BuiltInCodeExecutor extends BaseCodeExecutor {
}

processLlmRequest(llmRequest: LlmRequest) {
if (llmRequest.model && isGemini2Model(llmRequest.model)) {
if (llmRequest.model && isGemini2OrLaterModel(llmRequest.model)) {
llmRequest.config = llmRequest.config || {};
llmRequest.config.tools = llmRequest.config.tools || [];
llmRequest.config.tools.push({codeExecution: {}});
Expand Down
24 changes: 23 additions & 1 deletion core/src/utils/model_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const MODEL_NAME_PATTERN =
'^projects/[^/]+/locations/[^/]+/publishers/[^/]+/models/(.+)$';
'^projects/[^/]+/locations/[^/]+/publishers/[^/]+/models/(.+)$';

/**
* Extract the actual model name from either simple or path-based format.
Expand Down Expand Up @@ -58,4 +58,26 @@ export function isGemini2Model(modelString: string): boolean {
const modelName = extractModelName(modelString);

return modelName.startsWith('gemini-2');
}

/**
* Check if the model is a Gemini 3.x model using regex patterns.
*
* @param modelString Either a simple model name or path - based model name
* @return true if it's a Gemini 3.x model, false otherwise.
*/
export function isGemini3Model(modelString: string): boolean {
const modelName = extractModelName(modelString);

return modelName.startsWith('gemini-3');
}

/**
* Check if the model is Gemini 2.0 or later (includes 2.x and 3.x).
*
* @param modelString Either a simple model name or path - based model name
* @return true if it's Gemini 2.0+, false otherwise.
*/
export function isGemini2OrLaterModel(modelString: string): boolean {
return isGemini2Model(modelString) || isGemini3Model(modelString);
}