Fix structured output multi-turn conversation error#531
Open
alexey-hunter-io wants to merge 3 commits intocrmne:mainfrom
Open
Fix structured output multi-turn conversation error#531alexey-hunter-io wants to merge 3 commits intocrmne:mainfrom
alexey-hunter-io wants to merge 3 commits intocrmne:mainfrom
Conversation
When using with_schema for structured output and continuing a conversation, the persisted Hash response was being passed directly to OpenAI instead of being serialized as JSON string. OpenAI API expects message content to be a string, so structured output Hashes stored in content_raw must be converted to JSON when replayed. Fixes crmne#497
nerlichman
reviewed
Jan 7, 2026
nerlichman
left a comment
There was a problem hiding this comment.
Thanks!
We faced the same issue on a project and applied locally the changes suggested in this PR, and this fixed our issue.
Comment on lines
12
to
15
| return content.value.is_a?(Hash) ? content.value.to_json : content.value | ||
| end | ||
| return content.to_json if content.is_a?(Hash) || content.is_a?(Array) | ||
| return content unless content.is_a?(Content) |
There was a problem hiding this comment.
Looks like the same logic is done on Content::Raw for Hash/Array but using value instead of directly using content.
Maybe something like this could work too:
value = content.is_a?(RubyLLM::Content::Raw) ? content.value : content
return value.to_json if value.is_a?(Hash) || value.is_a?(Array)
return content unless content.is_a?(Content)
Author
There was a problem hiding this comment.
Thanks! I've added an array check for consistency
3 tasks
Author
|
@crmne Is there anything I can do to help get this fix merged? Thanks! |
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.
What this does
Fixes multi-turn conversation error when using
with_schemafor structured output. The persisted Hash/Array response was being passed directly to OpenAI instead of being serialized as a JSON string.OpenAI API expects message content to be a string, so structured output Hashes/Arrays stored in
content_rawmust be converted to JSON when replayed.Type of change
Scope check
Quality check
overcommit --installand all hooks passbundle exec rake vcr:record[provider_name]bundle exec rspecmodels.json,aliases.json)API changes
Related issues
Fixes #497