fix: Allow two or more agent calls to Gemini (#10214)

* fix: Allow two or more agent calls to Gemini

* [autofix.ci] apply automated fixes

* Update google_generative_ai.py

* Update google_generative_ai.py

* Update google_generative_ai.py

* chore: update component index

* Update google_generative_ai.py

* chore: update component index

* Update component_index.json

* chore: update component index

* Update component_index.json

* chore: update component index

* Update language_model.py

* chore: update component index

* [autofix.ci] apply automated fixes

* chore: update component index

* Update component_index.json

* chore: update component index

* Reorganize the imports for the fixed Google model

* Fix templates

* Update component_index.json

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Eric Hare
2025-10-15 10:51:17 -07:00
committed by GitHub
parent 87351d28df
commit 9de0bc5848
3 changed files with 44 additions and 9 deletions

View File

@ -0,0 +1,38 @@
from langchain_google_genai import ChatGoogleGenerativeAI
class ChatGoogleGenerativeAIFixed(ChatGoogleGenerativeAI):
"""Custom ChatGoogleGenerativeAI that fixes function response name issues for Gemini."""
def __init__(self, *args, **kwargs):
"""Initialize with fix for empty function response names in ToolMessage and FunctionMessage."""
if ChatGoogleGenerativeAI is None:
msg = "The 'langchain_google_genai' package is required to use the Google Generative AI model."
raise ImportError(msg)
# Initialize the parent class
super().__init__(*args, **kwargs)
def _prepare_request(self, messages, **kwargs):
"""Override request preparation to fix empty function response names."""
from langchain_core.messages import FunctionMessage, ToolMessage
# Pre-process messages to ensure tool/function messages have names
fixed_messages = []
for message in messages:
fixed_message = message
if isinstance(message, ToolMessage) and not message.name:
# Create a new ToolMessage with a default name
fixed_message = ToolMessage(
content=message.content,
name="tool_response",
tool_call_id=getattr(message, "tool_call_id", None),
artifact=getattr(message, "artifact", None),
)
elif isinstance(message, FunctionMessage) and not message.name:
# Create a new FunctionMessage with a default name
fixed_message = FunctionMessage(content=message.content, name="function_response")
fixed_messages.append(fixed_message)
# Call the parent's method with fixed messages
return super()._prepare_request(fixed_messages, **kwargs)

View File

@ -4,6 +4,7 @@ import requests
from pydantic.v1 import SecretStr
from lfx.base.models.google_generative_ai_constants import GOOGLE_GENERATIVE_AI_MODELS
from lfx.base.models.google_generative_ai_model import ChatGoogleGenerativeAIFixed
from lfx.base.models.model import LCModelComponent
from lfx.field_typing import LanguageModel
from lfx.field_typing.range_spec import RangeSpec
@ -74,12 +75,6 @@ class GoogleGenerativeAIComponent(LCModelComponent):
]
def build_model(self) -> LanguageModel: # type: ignore[type-var]
try:
from langchain_google_genai import ChatGoogleGenerativeAI
except ImportError as e:
msg = "The 'langchain_google_genai' package is required to use the Google Generative AI model."
raise ImportError(msg) from e
google_api_key = self.api_key
model = self.model_name
max_output_tokens = self.max_output_tokens
@ -88,7 +83,9 @@ class GoogleGenerativeAIComponent(LCModelComponent):
top_p = self.top_p
n = self.n
return ChatGoogleGenerativeAI(
# Use modified ChatGoogleGenerativeAIFixed class for multiple function support
# TODO: Potentially remove when fixed upstream
return ChatGoogleGenerativeAIFixed(
model=model,
max_output_tokens=max_output_tokens or None,
temperature=temperature,

View File

@ -1,11 +1,11 @@
from typing import Any
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_openai import ChatOpenAI
from lfx.base.models.anthropic_constants import ANTHROPIC_MODELS
from lfx.base.models.google_generative_ai_constants import GOOGLE_GENERATIVE_AI_MODELS
from lfx.base.models.google_generative_ai_model import ChatGoogleGenerativeAIFixed
from lfx.base.models.model import LCModelComponent
from lfx.base.models.openai_constants import OPENAI_CHAT_MODEL_NAMES, OPENAI_REASONING_MODEL_NAMES
from lfx.field_typing import LanguageModel
@ -112,7 +112,7 @@ class LanguageModelComponent(LCModelComponent):
if not self.api_key:
msg = "Google API key is required when using Google provider"
raise ValueError(msg)
return ChatGoogleGenerativeAI(
return ChatGoogleGenerativeAIFixed(
model=model_name,
temperature=temperature,
streaming=stream,