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
Unified · Split
| @@ -309,9 +309,19 @@ impl ToolUseState { · 308 unmodified lines | ||
| 309 | 309 | if let Some(tool_uses) = self.tool_uses_by_user_message.get(&message_id) { |
| 310 | 310 | for tool_use_id in tool_uses { |
| 311 | 311 | 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 | + )); | |
| 315 | 325 | } |
| 316 | 326 | } |
| 317 | 327 | } |