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 (#900)

* Empty text content should not be set when content is nil when using AnthropicMessage

* CHANGELOG entry
  • Loading branch information
andreibondarev authored Dec 16, 2024
1 parent 9bbd609 commit 41e232b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [SECURITY]: A change which fixes a security vulnerability.

## [Unreleased]
- [BUGFIX] [https://github.com/patterns-ai-core/langchainrb/pull/900] Empty text content should not be set when content is nil when using AnthropicMessage

## [0.19.2] - 2024-11-26
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/884] Add `tool_execution_callback` to `Langchain::Assistant`, a callback function (proc, lambda) that is called right before a tool is executed
Expand Down
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}
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 41e232b

Please sign in to comment.