Fix structured output multi-turn conversation error#531
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
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.
| 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)There was a problem hiding this comment.
Thanks! I've added an array check for consistency
|
@crmne Is there anything I can do to help get this fix merged? Thanks! |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #531 +/- ##
=======================================
Coverage 80.95% 80.95%
=======================================
Files 114 114
Lines 5213 5215 +2
Branches 1351 1353 +2
=======================================
+ Hits 4220 4222 +2
Misses 993 993 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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