Fix Assistant OpenAI adapter to handle message content structure returned by to_hash method#952
Open
IMhide wants to merge 3 commits intopatterns-ai-core:mainfrom
Open
Fix Assistant OpenAI adapter to handle message content structure returned by to_hash method#952IMhide wants to merge 3 commits intopatterns-ai-core:mainfrom
IMhide wants to merge 3 commits intopatterns-ai-core:mainfrom
Conversation
… order to accept .to_hash content array Adding test case
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an issue with the OpenAI adapter in Langchain where messages returned by the to_hash method were being stringified when re-added to the assistant. The changes update the build_message method to handle an array of content hashes and add tests to verify both string and array content scenarios.
- Updated build_message to process content arrays by extracting text and image URL values.
- Extended the unit tests to cover both string and array-based message content.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| spec/langchain/assistant/llm/adapters/openai_spec.rb | Added tests for build_message with string and array content |
| lib/langchain/assistant/llm/adapters/openai.rb | Updated build_message to process array content as expected |
Comments suppressed due to low confidence (2)
lib/langchain/assistant/llm/adapters/openai.rb:43
- Consider adding explicit handling or documentation for scenarios where the content array contains multiple 'text' or 'image_url' elements, as the current implementation will override earlier values in favor of later ones.
if content.is_a?(Array)
spec/langchain/assistant/llm/adapters/openai_spec.rb:50
- [nitpick] Consider adding tests to cover scenarios where the array contains multiple entries for 'text' or 'image_url' to ensure the intended behavior is maintained.
context "when content is an array" do
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While using the gem for the first time I was discovering
Langchain::Assistantand experimenting with it.While using naively I came across an issue :
Here
message_hashequalFrom there I was thinking great I can just persist
messages_hashsomewhere and then use the method@assistant.add_messagesto resume the conversationBut this happened :
resumed_hashequal thisThe
message_hashget stringified and nested into a new hashAfter some investigation I found out that this behaviour comes from the function
build_messageat/lib/langchain/assistant/llm/adapters/openai.rb:42That ignore the fact that the method
to_hash, from the same object, transformcontentinto an hash that merge text message and image urlThis PR is extracted from a monkey patch I made in my project :
I also added tests for the
#build_messagethat covers the previous behavior and the one added