mirror of
https://github.com/hwchase17/langchain.git
synced 2026-07-13 12:14:06 +00:00
fix(core): raise outputparserexception for unknown tools (#34923)
This commit is contained in:
@@ -355,8 +355,18 @@ class PydanticToolsParser(JsonOutputToolsParser):
|
||||
f"{res['args']}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
try:
|
||||
pydantic_objects.append(name_dict[res["type"]](**res["args"]))
|
||||
tool = name_dict[res["type"]]
|
||||
except KeyError as e:
|
||||
available = ", ".join(name_dict.keys()) or "<no_tools>"
|
||||
msg = (
|
||||
f"Unknown tool type: {res['type']!r}. Available tools: {available}"
|
||||
)
|
||||
raise OutputParserException(msg) from e
|
||||
|
||||
try:
|
||||
pydantic_objects.append(tool(**res["args"]))
|
||||
except (ValidationError, ValueError):
|
||||
if partial:
|
||||
continue
|
||||
|
||||
@@ -6,6 +6,7 @@ import pydantic
|
||||
import pytest
|
||||
from pydantic import BaseModel, Field, ValidationError
|
||||
|
||||
from langchain_core.exceptions import OutputParserException
|
||||
from langchain_core.messages import (
|
||||
AIMessage,
|
||||
AIMessageChunk,
|
||||
@@ -1423,3 +1424,31 @@ def test_parse_tool_call_partial_mode_with_none_arguments() -> None:
|
||||
|
||||
# In partial mode, None arguments returns None (incomplete tool call)
|
||||
assert result is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("partial", [False, True])
|
||||
def test_pydantic_tools_parser_unknown_tool_raises_output_parser_exception(
|
||||
partial: bool, # noqa: FBT001
|
||||
) -> None:
|
||||
class KnownTool(BaseModel):
|
||||
value: int
|
||||
|
||||
parser = PydanticToolsParser(tools=[KnownTool])
|
||||
message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[
|
||||
{
|
||||
"id": "call_unknown",
|
||||
"name": "UnknownTool",
|
||||
"args": {"value": 1},
|
||||
}
|
||||
],
|
||||
)
|
||||
generation = ChatGeneration(message=message)
|
||||
|
||||
with pytest.raises(OutputParserException) as excinfo:
|
||||
parser.parse_result([generation], partial=partial)
|
||||
|
||||
msg = str(excinfo.value)
|
||||
assert "Unknown tool type" in msg
|
||||
assert "UnknownTool" in msg
|
||||
|
||||
Reference in New Issue
Block a user