mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-08 05:39:13 +00:00
Drop tool call, result without tool id on call to Anthropic, Openai APIs
This commit is contained in:
@@ -308,6 +308,10 @@ def format_messages_for_anthropic(raw_messages: list[ChatMessage], system_prompt
|
|||||||
# Convert tool_result to Anthropic tool_result format
|
# Convert tool_result to Anthropic tool_result format
|
||||||
content = []
|
content = []
|
||||||
for part in message.content:
|
for part in message.content:
|
||||||
|
# Skip tool results without valid tool_use_id as Anthropic API requires string IDs
|
||||||
|
if not part.get("id"):
|
||||||
|
logger.warning(f"Dropping tool result without valid tool_use_id: {part.get('name')}")
|
||||||
|
continue
|
||||||
content.append(
|
content.append(
|
||||||
{
|
{
|
||||||
"type": "tool_result",
|
"type": "tool_result",
|
||||||
|
|||||||
@@ -372,6 +372,10 @@ def format_message_for_api(raw_messages: List[ChatMessage], api_base_url: str) -
|
|||||||
# Convert tool_call to OpenAI function call format
|
# Convert tool_call to OpenAI function call format
|
||||||
content = []
|
content = []
|
||||||
for part in message.content:
|
for part in message.content:
|
||||||
|
# Skip tool calls without valid IDs as OpenAI API requires string IDs
|
||||||
|
if not part.get("id"):
|
||||||
|
logger.warning(f"Dropping tool call without valid ID: {part.get('name')}")
|
||||||
|
continue
|
||||||
content.append(
|
content.append(
|
||||||
{
|
{
|
||||||
"type": "function",
|
"type": "function",
|
||||||
@@ -382,22 +386,31 @@ def format_message_for_api(raw_messages: List[ChatMessage], api_base_url: str) -
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
formatted_messages.append(
|
# Only add the message if there are valid tool calls
|
||||||
{
|
if content:
|
||||||
"role": "assistant",
|
formatted_messages.append(
|
||||||
"content": None,
|
{
|
||||||
"tool_calls": content,
|
"role": "assistant",
|
||||||
}
|
"content": None,
|
||||||
)
|
"tool_calls": content,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.warning("Dropping tool call message with no valid tool calls")
|
||||||
continue
|
continue
|
||||||
if message_type == "tool_result":
|
if message_type == "tool_result":
|
||||||
# Convert tool_result to OpenAI tool result format
|
# Convert tool_result to OpenAI tool result format
|
||||||
# Each part is a result for a tool call
|
# Each part is a result for a tool call
|
||||||
for part in message.content:
|
for part in message.content:
|
||||||
|
tool_call_id = part.get("id") or part.get("tool_use_id")
|
||||||
|
# Skip tool results without valid tool_call_id as OpenAI API requires string IDs
|
||||||
|
if not tool_call_id:
|
||||||
|
logger.warning(f"Dropping tool result without valid tool_call_id: {part.get('name')}")
|
||||||
|
continue
|
||||||
formatted_messages.append(
|
formatted_messages.append(
|
||||||
{
|
{
|
||||||
"role": "tool",
|
"role": "tool",
|
||||||
"tool_call_id": part.get("id") or part.get("tool_use_id"),
|
"tool_call_id": tool_call_id,
|
||||||
"name": part.get("name"),
|
"name": part.get("name"),
|
||||||
"content": part.get("content"),
|
"content": part.get("content"),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user