diff --git a/libs/langchain-google-common/src/chat_models.ts b/libs/langchain-google-common/src/chat_models.ts index 1de5280fbe72..7d476b94cf2c 100644 --- a/libs/langchain-google-common/src/chat_models.ts +++ b/libs/langchain-google-common/src/chat_models.ts @@ -469,6 +469,7 @@ export abstract class ChatGoogleBase } const llm = this.bind({ tools, + tool_choice: functionName, }); if (!includeRaw) { diff --git a/libs/langchain-google-common/src/utils/gemini.ts b/libs/langchain-google-common/src/utils/gemini.ts index 48bd41fb5c2f..23c46f3783db 100644 --- a/libs/langchain-google-common/src/utils/gemini.ts +++ b/libs/langchain-google-common/src/utils/gemini.ts @@ -1047,10 +1047,20 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI { return undefined; } + if (["auto", "any", "none"].includes(parameters.tool_choice)) { + return { + functionCallingConfig: { + mode: parameters.tool_choice as "auto" | "any" | "none", + allowedFunctionNames: parameters.allowed_function_names, + }, + }; + } + + // force tool choice to be a single function name in case of structured output return { functionCallingConfig: { - mode: parameters.tool_choice as "auto" | "any" | "none", - allowedFunctionNames: parameters.allowed_function_names, + mode: "any", + allowedFunctionNames: [parameters.tool_choice], }, }; }