History

assistant2: Handle empty tool results by providing placeholder text (#27130)

This is surprising, but the Anthropic API returns a 400 if a tool output is an empty string because it thinks we're attaching a `tool use` without a corresponding `tool result`, but we are not, it's just empty (which seems totally reasonable) 🙃 Release Notes: - N/A

Agus Zubiaga committed 1 year ago

Commit dfdca541 parent 14c03691 file · +13 −3Browse files

Unified · Split

crates/assistant2/src/tool_use.rs+13−3
@@ -309,9 +309,19 @@ impl ToolUseState { · 308 unmodified lines
309309 if let Some(tool_uses) = self.tool_uses_by_user_message.get(&message_id) {
310310 for tool_use_id in tool_uses {
311311 if let Some(tool_result) = self.tool_results.get(tool_use_id) {
312− request_message
313− .content
314− .push(MessageContent::ToolResult(tool_result.clone()));
312+ request_message.content.push(MessageContent::ToolResult(
313+ LanguageModelToolResult {
314+ tool_use_id: tool_use_id.clone(),
315+ is_error: tool_result.is_error,
316+ content: if tool_result.content.is_empty() {
317+ // Surprisingly, the API fails if we return an empty string here.
318+ // It thinks we are sending a tool use without a tool result.
319+ "<Tool returned an empty string>".into()
320+ } else {
321+ tool_result.content.clone()
322+ },
323+ },
324+ ));
315325 }
316326 }
317327 }