Skip to content

Conversation

@alexey-hunter-io
Copy link

Summary

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 a 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.

Changes

  • Modified lib/ruby_llm/providers/openai/media.rb to convert Hash values to JSON strings in format_content
  • Added test case for multi-turn conversations with structured responses

Root Cause

In format_content, when content is a Content::Raw object containing a Hash (from structured output), the Hash was returned directly:

return content.value if content.is_a?(RubyLLM::Content::Raw)

But OpenAI rejects this with: "Invalid type for 'messages[N].content': expected one of a string or array of objects, but got an object instead."

Fix

if content.is_a?(RubyLLM::Content::Raw)
  return content.value.is_a?(Hash) ? content.value.to_json : content.value
end

Fixes #497

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Schema breaks multi-turn conversations after persistence

2 participants