-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve error handling of Agent component, solves Empty ExceptionWithMessageError #6097
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bf61f70
Gracefully handle Errors
edwinjosechittilappilly 61480b7
updates to Error handling
edwinjosechittilappilly 6b37a5e
update in Error handling
edwinjosechittilappilly e67ee32
update lint error similar to main
edwinjosechittilappilly 4595f40
Merge branch 'main' into fix-agent-errors
edwinjosechittilappilly 93fc8f8
[autofix.ci] apply automated fixes
autofix-ci[bot] 46e3f71
[autofix.ci] apply automated fixes (attempt 2/3)
autofix-ci[bot] b5889ad
feat: add max retry and request timeout to open ai component, fixes r…
edwinjosechittilappilly b600417
update to __str__ and fix lint errors
edwinjosechittilappilly 52cf843
Merge branch 'main' into fix-agent-errors
edwinjosechittilappilly a1c591e
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from anthropic import BadRequestError as AnthropicBadRequestError | ||
from cohere import BadRequestError as CohereBadRequestError | ||
from httpx import HTTPStatusError | ||
|
||
from langflow.schema.message import Message | ||
|
||
|
||
class CustomBadRequestError(AnthropicBadRequestError, CohereBadRequestError, HTTPStatusError): | ||
def __init__(self, agent_message: Message | None, message: str): | ||
super().__init__(message) | ||
self.message = message | ||
self.agent_message = agent_message | ||
|
||
def __str__(self): | ||
return f"{self.message}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from langchain_core.tools import StructuredTool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from langflow.base.agents.agent import LCToolsAgentComponent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from langflow.base.agents.events import ExceptionWithMessageError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from langflow.base.models.model_input_constants import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ALL_PROVIDER_FIELDS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MODEL_DYNAMIC_UPDATE_FIELDS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -65,43 +66,32 @@ class AgentComponent(ToolCallingAgentComponent): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
async def message_response(self) -> Message: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Get LLM model and validate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
llm_model, display_name = self.get_llm() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if llm_model is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = "No language model selected" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = "No language model selected. Please choose a model to proceed." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.model_name = get_model_name(llm_model, display_name=display_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Log the error for debugging purposes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"Error retrieving language model: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Get memory data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.chat_history = await self.get_memory_data() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"Error retrieving chat history: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if self.add_current_date_tool: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Add current date tool if enabled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if self.add_current_date_tool: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not isinstance(self.tools, list): # type: ignore[has-type] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.tools = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Convert CurrentDateComponent to a StructuredTool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
current_date_tool = (await CurrentDateComponent(**self.get_base_args()).to_toolkit()).pop(0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if isinstance(current_date_tool, StructuredTool): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.tools.append(current_date_tool) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not isinstance(current_date_tool, StructuredTool): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = "CurrentDateComponent must be converted to a StructuredTool" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise TypeError(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"Error adding current date tool: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.tools.append(current_date_tool) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not self.tools: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = "Tools are required to run the agent." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Validate tools | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not self.tools: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = "Tools are required to run the agent. Please add at least one tool." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Set up and run agent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.set( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
llm=llm_model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tools=self.tools, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -110,12 +100,18 @@ async def message_response(self) -> Message: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
system_prompt=self.system_prompt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
agent = self.create_agent_runnable() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return await self.run_agent(agent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except (ValueError, TypeError, KeyError) as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"{type(e).__name__}: {e!s}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except ExceptionWithMessageError as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"ExceptionWithMessageError occurred: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"Error setting up the agent: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"Unexpected error: {e!s}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return await self.run_agent(agent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
async def get_memory_data(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
memory_kwargs = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
component_input.name: getattr(self, f"{component_input.name}") for component_input in self.memory_inputs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -126,22 +122,26 @@ async def get_memory_data(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return await MemoryComponent(**self.get_base_args()).set(**memory_kwargs).retrieve_messages() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def get_llm(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if isinstance(self.agent_llm, str): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if provider_info: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
component_class = provider_info.get("component_class") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
display_name = component_class.display_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inputs = provider_info.get("inputs") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix = provider_info.get("prefix", "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self._build_llm_model(component_class, inputs, prefix), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
display_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = f"Error building {self.agent_llm} language model" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError(msg) from e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self.agent_llm, None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not isinstance(self.agent_llm, str): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self.agent_llm, None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
provider_info = MODEL_PROVIDERS_DICT.get(self.agent_llm) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not provider_info: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = f"Invalid model provider: {self.agent_llm}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
component_class = provider_info.get("component_class") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
display_name = component_class.display_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inputs = provider_info.get("inputs") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix = provider_info.get("prefix", "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self._build_llm_model(component_class, inputs, prefix), display_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(f"Error building {self.agent_llm} language model: {e!s}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msg = f"Failed to initialize language model: {e!s}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+125
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError(msg) from e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def _build_llm_model(self, component, inputs, prefix=""): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
model_kwargs = {input_.name: getattr(self, f"{prefix}{input_.name}") for input_ in inputs} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like the diff is messed up here. I suppose there was a real optimization here but the output seems incorrect. This is happening due to our dependency on a diffing library that is buggy. We are looking to fix this