Skip to content

Commit

Permalink
fix: validation for tool name (#4790)
Browse files Browse the repository at this point in the history
validation for tool name

Tool name validation based on open ai Schema for tool names
  • Loading branch information
edwinjosechittilappilly authored Nov 22, 2024
1 parent f440552 commit 230a019
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/backend/base/langflow/base/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import re
from abc import abstractmethod
from typing import TYPE_CHECKING, cast

Expand Down Expand Up @@ -178,6 +179,18 @@ async def run_agent(
def create_agent_runnable(self) -> Runnable:
"""Create the agent."""

def validate_tool_names(self) -> None:
"""Validate tool names to ensure they match the required pattern."""
pattern = re.compile(r"^[a-zA-Z0-9_-]+$")
if hasattr(self, "tools") and self.tools:
for tool in self.tools:
if not pattern.match(tool.name):
msg = (
f"Invalid tool name '{tool.name}': must only contain letters, numbers, underscores, dashes,"
" and cannot contain spaces."
)
raise ValueError(msg)


class LCToolsAgentComponent(LCAgentComponent):
_base_inputs = [
Expand All @@ -193,6 +206,7 @@ class LCToolsAgentComponent(LCAgentComponent):
]

def build_agent(self) -> AgentExecutor:
self.validate_tool_names()
agent = self.create_agent_runnable()
return AgentExecutor.from_agent_and_tools(
agent=RunnableAgent(runnable=agent, input_keys_arg=["input"], return_keys_arg=["output"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def create_agent_runnable(self):
("placeholder", "{agent_scratchpad}"),
]
prompt = ChatPromptTemplate.from_messages(messages)
self.validate_tool_names()
try:
return create_tool_calling_agent(self.llm, self.tools or [], prompt)
except NotImplementedError as e:
Expand Down

0 comments on commit 230a019

Please sign in to comment.