mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
core[minor], integrations...[patch]: Support ToolCall as Tool input and ToolMessage as Tool output (#24038)
Changes:
- ToolCall, InvalidToolCall and ToolCallChunk can all accept a "type"
parameter now
- LLM integration packages add "type" to all the above
- Tool supports ToolCall inputs that have "type" specified
- Tool outputs ToolMessage when a ToolCall is passed as input
- Tools can separately specify ToolMessage.content and
ToolMessage.raw_output
- Tools emit events for validation errors (using on_tool_error and
on_tool_end)
Example:
```python
@tool("structured_api", response_format="content_and_raw_output")
def _mock_structured_tool_with_raw_output(
arg1: int, arg2: bool, arg3: Optional[dict] = None
) -> Tuple[str, dict]:
"""A Structured Tool"""
return f"{arg1} {arg2}", {"arg1": arg1, "arg2": arg2, "arg3": arg3}
def test_tool_call_input_tool_message_with_raw_output() -> None:
tool_call: Dict = {
"name": "structured_api",
"args": {"arg1": 1, "arg2": True, "arg3": {"img": "base64string..."}},
"id": "123",
"type": "tool_call",
}
expected = ToolMessage("1 True", raw_output=tool_call["args"], tool_call_id="123")
tool = _mock_structured_tool_with_raw_output
actual = tool.invoke(tool_call)
assert actual == expected
tool_call.pop("type")
with pytest.raises(ValidationError):
tool.invoke(tool_call)
actual_content = tool.invoke(tool_call["args"])
assert actual_content == expected.content
```
---------
Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
@@ -50,6 +50,7 @@ from langchain_core.messages import (
|
||||
ToolCall,
|
||||
ToolMessage,
|
||||
)
|
||||
from langchain_core.messages.tool import tool_call_chunk
|
||||
from langchain_core.output_parsers import (
|
||||
JsonOutputParser,
|
||||
PydanticOutputParser,
|
||||
@@ -103,19 +104,10 @@ def _convert_mistral_chat_message_to_message(
|
||||
dict, parse_tool_call(raw_tool_call, return_id=True)
|
||||
)
|
||||
if not parsed["id"]:
|
||||
tool_call_id = uuid.uuid4().hex[:]
|
||||
tool_calls.append(
|
||||
{
|
||||
**parsed,
|
||||
**{"id": tool_call_id},
|
||||
},
|
||||
)
|
||||
else:
|
||||
tool_calls.append(parsed)
|
||||
parsed["id"] = uuid.uuid4().hex[:]
|
||||
tool_calls.append(parsed)
|
||||
except Exception as e:
|
||||
invalid_tool_calls.append(
|
||||
dict(make_invalid_tool_call(raw_tool_call, str(e)))
|
||||
)
|
||||
invalid_tool_calls.append(make_invalid_tool_call(raw_tool_call, str(e)))
|
||||
return AIMessage(
|
||||
content=content,
|
||||
additional_kwargs=additional_kwargs,
|
||||
@@ -206,12 +198,12 @@ def _convert_chunk_to_message_chunk(
|
||||
else:
|
||||
tool_call_id = raw_tool_call.get("id")
|
||||
tool_call_chunks.append(
|
||||
{
|
||||
"name": raw_tool_call["function"].get("name"),
|
||||
"args": raw_tool_call["function"].get("arguments"),
|
||||
"id": tool_call_id,
|
||||
"index": raw_tool_call.get("index"),
|
||||
}
|
||||
tool_call_chunk(
|
||||
name=raw_tool_call["function"].get("name"),
|
||||
args=raw_tool_call["function"].get("arguments"),
|
||||
id=tool_call_id,
|
||||
index=raw_tool_call.get("index"),
|
||||
)
|
||||
)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user