Skip to content
Open
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
27 changes: 19 additions & 8 deletions core/llm/openaiTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,25 @@ export function toChatMessage(

// Add tool calls if present
if (message.toolCalls) {
msg.tool_calls = message.toolCalls.map((toolCall) => ({
id: toolCall.id!,
type: toolCall.type!,
function: {
name: toolCall.function?.name!,
arguments: toolCall.function?.arguments || "{}",
},
}));
msg.tool_calls = message.toolCalls.map((toolCall) => {
// Sanitize arguments — ensure valid JSON to prevent vLLM 400 errors
// when malformed tool arguments from previous turns are sent back in history
let args = toolCall.function?.arguments || "{}";
try {
JSON.parse(args);
} catch {
// If arguments are not valid JSON, wrap them to prevent server rejection
args = JSON.stringify({ _raw: args });
}
return {
id: toolCall.id!,
type: toolCall.type!,
function: {
name: toolCall.function?.name!,
arguments: args,
},
};
});
}

// Preserving reasoning blocks
Expand Down
Loading