Skip to content

Commit

Permalink
Empty text content should not be set when content is nil when using A…
Browse files Browse the repository at this point in the history
…nthropicMessage
  • Loading branch information
andreibondarev committed Dec 16, 2024
1 parent 9bbd609 commit c74053a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/langchain/assistant/messages/anthropic_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def to_hash
#
# @return [Hash] The message as an Anthropic API-compatible hash, with the role as "assistant"
def assistant_hash
content_array = []
if content && !content.empty?
content_array << { type: "text", text: content }

Check failure on line 58 in lib/langchain/assistant/messages/anthropic_message.rb

View workflow job for this annotation

GitHub Actions / linters

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 58 in lib/langchain/assistant/messages/anthropic_message.rb

View workflow job for this annotation

GitHub Actions / linters

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.
end

{
role: "assistant",
content: [
{
type: "text",
text: content
}
].concat(tool_calls)
content: content_array.concat(tool_calls)
}
end

Expand Down
34 changes: 34 additions & 0 deletions spec/langchain/assistant/messages/anthropic_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,40 @@
]
)
end

it "returns assistant_hash with tool_calls without content" do
message = described_class.new(
role: role,
content: nil,
tool_calls: [
{
"type" => "tool_use",
"id" => "toolu_01UEciZACvRZ6S4rqAwD1syH",
"name" => "news_retriever__get_everything",
"input" => {
"q" => "Google I/O 2024",
"sort_by" => "publishedAt",
"language" => "en"
}
}
]
)
expect(message.to_hash).to eq(
role: role,
content: [
{
"type" => "tool_use",
"id" => "toolu_01UEciZACvRZ6S4rqAwD1syH",
"name" => "news_retriever__get_everything",
"input" => {
"q" => "Google I/O 2024",
"sort_by" => "publishedAt",
"language" => "en"
}
}
]
)
end
end

context "when role is tool_result" do
Expand Down

0 comments on commit c74053a

Please sign in to comment.