Fix #377: Resolve OpenAI client streaming/non-streaming parser mixing #446
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.
Fix #377: Resolve OpenAI client streaming/non-streaming parser mixing
Problem
The OpenAI client gets "stuck" in streaming or non-streaming mode after switching between
stream=Trueandstream=Falsecalls. This happens because theself.response_parserinstance variable is modified during each call and persists across subsequent calls, causing the wrong parser to be used.Root Cause
The issue occurs in these lines:
self.response_parser = self.streaming_response_parser_sync(for streaming)self.response_parser = self.non_streaming_response_parser(for non-streaming)The instance variable assignment means:
generator(stream=True)→ Setsself.response_parser = streaming_parsergenerator(stream=False)→ Setsself.response_parser = non_streaming_parsergenerator(stream=True)→ Butparse_chat_completion()still uses the oldself.response_parserSolution
parse_chat_completion()self.response_parserassignmentsBenefits
Testing
Code Changes
parse_chat_completion(): Dynamic parser selection based on completion typeself.response_parserassignmentself.response_parserassignmentType of Change
Impact
This fix resolves a critical issue that made the OpenAI client unreliable when users needed to dynamically switch between streaming and non-streaming modes in the same application session.
Fixes #377