Skip to content

Commit ea4e190

Browse files
TheBergsergiobayonaandreibondarev
authored
fix(Anthropic): Handle empty tool input (#960)
* fix(Anthropic): Handle empty tool input * Fix linting errors --------- Co-authored-by: Sergio Bayona <[email protected]> Co-authored-by: Andrei Bondarev <[email protected]>
1 parent 173a5fa commit ea4e190

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/langchain/llm/anthropic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def tool_calls_from_choice_chunks(chunks)
177177
"id" => first_block.dig("content_block", "id"),
178178
"type" => "tool_use",
179179
"name" => first_block.dig("content_block", "name"),
180-
"input" => JSON.parse(input).transform_keys(&:to_sym)
180+
"input" => input.empty? ? nil : JSON.parse(input).transform_keys(&:to_sym)
181181
}
182182
end.compact
183183
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{"type": "message_start", "message": {"id": "msg_014p7gG3wDgGV9EUtLvnow3U", "type": "message", "role": "assistant", "model": "claude-3-haiku-20240307", "content": [], "stop_reason": null}},
3+
{"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},
4+
{"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "I'll check the weather for you:"}},
5+
{"type": "content_block_stop", "index": 0},
6+
{"type": "content_block_start", "index": 1, "content_block": {"type": "tool_use", "id": "toolu_01T1x1fJ34qAmk2tNTrN7Up6", "name": "get_weather", "input": {}}},
7+
{"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": ""}},
8+
{"type": "content_block_stop", "index": 1},
9+
{"type": "message_delta", "delta": {"stop_reason": "tool_use", "stop_sequence": null}, "usage": {"output_tokens": 10}},
10+
{"type": "message_stop"}
11+
]

spec/langchain/llm/anthropic_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@
175175
expect(rsp.tool_calls.first["name"]).to eq("get_weather")
176176
expect(rsp.tool_calls.first["input"]).to eq({location: "San Francisco, CA", unit: "fahrenheit"})
177177
end
178+
179+
context "response has empty input" do
180+
let(:fixture) { File.read("spec/fixtures/llm/anthropic/chat_stream_with_empty_tool_input.json") }
181+
182+
it "handles empty input in tool calls correctly" do
183+
# The test will pass if no exception is raised during processing
184+
rsp = subject.chat(messages: [{role: "user", content: "What's the weather?"}], &stream_handler)
185+
186+
# Verify the response
187+
expect(rsp).to be_a(Langchain::LLM::AnthropicResponse)
188+
expect(rsp.chat_completion).to eq("I'll check the weather for you:")
189+
190+
# Verify the tool call with empty input is handled correctly
191+
expect(rsp.tool_calls.first["name"]).to eq("get_weather")
192+
expect(rsp.tool_calls.first["input"]).to be_nil # Should be nil (null in Ruby) because input was empty
193+
end
194+
end
178195
end
179196
end
180197
end

0 commit comments

Comments
 (0)