Skip to content
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

allow no content if tool calls exist #27850

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions libs/core/langchain_core/messages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
from langchain_core.messages.human import HumanMessage, HumanMessageChunk
from langchain_core.messages.modifier import RemoveMessage
from langchain_core.messages.system import SystemMessage, SystemMessageChunk
from langchain_core.messages.tool import ToolCall, ToolMessage, ToolMessageChunk
from langchain_core.messages.tool import (
InvalidToolCall,
ToolCall,
ToolMessage,
ToolMessageChunk,
)

if TYPE_CHECKING:
from langchain_text_splitters import TextSplitter
Expand Down Expand Up @@ -317,9 +322,15 @@ def _convert_to_message(message: MessageLikeRepresentation) -> BaseMessage:
except KeyError:
msg_type = msg_kwargs.pop("type")
# None msg content is not allowed
msg_content = msg_kwargs.pop("content") or ""
content_or_tool_calls = (
"tool_calls" in msg_kwargs or "content" in msg_kwargs
)
if not content_or_tool_calls:
msg = "Must have one of content or tool calls"
raise KeyError(msg)
msg_content = msg_kwargs.pop("content", "") or ""
except KeyError as e:
msg = f"Message dict must contain 'role' and 'content' keys, got {message}"
msg = f"Message dict must contain 'role' and one of 'content' or 'tool_calls' keys, got {message}" # noqa: E501
msg = create_message(
message=msg, error_code=ErrorCode.MESSAGE_COERCION_FAILURE
)
Expand Down Expand Up @@ -957,6 +968,10 @@ def convert_to_openai_messages(
oai_msg["name"] = message.name
if isinstance(message, AIMessage) and message.tool_calls:
oai_msg["tool_calls"] = _convert_to_openai_tool_calls(message.tool_calls)
if isinstance(message, AIMessage) and message.invalid_tool_calls:
oai_msg["tool_calls"] = oai_msg.get(
"tool_calls", []
) + _convert_to_openai_tool_calls(message.invalid_tool_calls, invalid=True)
if message.additional_kwargs.get("refusal"):
oai_msg["refusal"] = message.additional_kwargs["refusal"]
if isinstance(message, ToolMessage):
Expand Down Expand Up @@ -1393,14 +1408,18 @@ def _get_message_openai_role(message: BaseMessage) -> str:
raise ValueError(msg)


def _convert_to_openai_tool_calls(tool_calls: list[ToolCall]) -> list[dict]:
def _convert_to_openai_tool_calls(
tool_calls: Union[list[ToolCall], list[InvalidToolCall]], invalid: bool = False
) -> list[dict]:
return [
{
"type": "function",
"id": tool_call["id"],
"function": {
"name": tool_call["name"],
"arguments": json.dumps(tool_call["args"]),
"arguments": tool_call["args"]
if invalid
else json.dumps(tool_call["args"]),
},
}
for tool_call in tool_calls
Expand Down
Loading