Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openai[patch]: support streaming with json_schema response format #29044

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ccurme
Copy link
Collaborator

@ccurme ccurme commented Jan 6, 2025

  • Stream JSON string content. Final chunk includes parsed representation (following OpenAI docs).
  • Mildly (?) breaking change: if you were using streaming with response_format before, usage metadata will disappear unless you set stream_usage=True.

Response format

Before:

Screenshot 2025-01-06 at 11 59 01 AM

After:

Screenshot 2025-01-06 at 11 58 13 AM

with_structured_output

For pydantic output, behavior of with_structured_output is unchanged (except for warning disappearing), because we pluck the parsed representation straight from OpenAI, and OpenAI doesn't return it until the stream is completed. Open to alternatives (e.g., parsing from content or intermediate dict chunks generated by OpenAI).

Before:

Screenshot 2025-01-06 at 12 38 11 PM

After:

Screenshot 2025-01-06 at 12 38 58 PM

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 6, 2025
Copy link

vercel bot commented Jan 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Jan 6, 2025 5:41pm

@dosubot dosubot bot added the langchain Related to the langchain package label Jan 6, 2025
@ccurme ccurme added 🔌: openai Primarily related to OpenAI integrations and removed langchain Related to the langchain package labels Jan 6, 2025
usage_metadata = chat_message.usage_metadata
else:
usage_metadata = None
message = AIMessageChunk(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we have a message to message chunk util

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I poked around and found:

  • message_chunk_to_message (I want to go the other way)
  • A private function _msg_to_chunk -- this function actually loses the parsed representation because it casts to/from dict. It's used in merge_message_runs which isn't used in monorepo, I see it's used in langchain-aws.

Let me know if you think it's important to add a util or I've missed it. Note I actually overwrite content to be "" so it's not a perfect copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔌: openai Primarily related to OpenAI integrations size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: Internal
Development

Successfully merging this pull request may close these issues.

2 participants