-
Notifications
You must be signed in to change notification settings - Fork 20.1k
fix(openai): preserve reasoning blocks in AIMessage and AIMessageChunk #34157
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
base: master
Are you sure you want to change the base?
fix(openai): preserve reasoning blocks in AIMessage and AIMessageChunk #34157
Conversation
CodSpeed Performance ReportMerging #34157 will not alter performanceComparing Summary
Footnotes
|
Filter out reasoning_content from user messages to match expected behavior in test__get_request_payload. Reasoning blocks should only be preserved for assistant messages from OpenAI compatible models.
|
is it possible to make this compatible with the OpenRouter format for passing reasoning details: for example see here |
|
@TomaszKaleczyc Thanks for the suggestion! This PR focuses specifically on fixing the missing reasoning blocks in OpenAI-compatible responses — ensuring that reasoning content is correctly preserved in AIMessage and AIMessageChunk. The reasoning_details field used by OpenRouter is an extended, provider-specific compatibility feature. I agree it’s valuable, but it would be cleaner to handle that as a separate, dedicated PR so it doesn’t scope-creep this fix. Happy to discuss approaches for supporting that extension in a follow-up PR! |
|
OK - but just want to put it out there this would really be valuable |
|
Happy to open a follow-up PR for OpenRouter-style reasoning_details once this one lands — just let me know if you’d like me to draft an RFC first. |
|
Does this work with the chat completions API? |
|
Yes — it works with the Chat Completions API. This PR preserves reasoning blocks in both If the provider sends reasoning content using the standard |
Fixes #34153
This PR ensures that reasoning blocks are preserved in
AIMessageandAIMessageChunkwhen using OpenAI compatible models. Previously, reasoning blocks in response content were being filtered out or not properly normalized, causing them to be missing from the message content.Changes:
_convert_dict_to_messageto detect and normalize reasoning blocks in content lists_format_message_contentto preserve reasoning blocks instead of filtering them_convert_delta_to_message_chunkto handle reasoning blocks in streaming scenariostest_reasoning_blocks_compatible_modelsto verify reasoning blocks are preservedTesting:
make format,make lint, andmake testinlibs/partners/openaiAI Usage:
Part of this change was drafted with the help of an AI assistant and then reviewed and edited by me.