fix(agents): normalize Gemini finish_reason values for the OpenAI SDK#372
Open
manana2520 wants to merge 1 commit into
Open
fix(agents): normalize Gemini finish_reason values for the OpenAI SDK#372manana2520 wants to merge 1 commit into
manana2520 wants to merge 1 commit into
Conversation
Gemini's OpenAI-compatible SSE responses carry non-OpenAI finish_reason values (STOP, MAX_TOKENS, SAFETY, RECITATION, ...) that the OpenAI SDK cannot deserialize, surfacing as run failures during wiki generation/chat with a Gemini-backed provider. Add FinishReasonNormalizingHandler as the outermost handler in the agent HttpClient chain (FinishReasonNormalizingHandler -> LoggingHttpHandler -> HttpClientHandler) so the streamed response is rewritten to the SDK's expected finish_reason set before deserialization. Non-Gemini providers are unaffected (values already valid pass through). Adds unit tests for the value mapping and the SSE line transform.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of #371 (tracking issue for upstreaming generic features from a self-hosted fork).
Problem
When an OpenAI-compatible provider is backed by Gemini, the streamed SSE responses carry non-OpenAI
finish_reasonvalues (STOP,MAX_TOKENS,SAFETY,RECITATION, …). The OpenAI SDK cannot deserialize these and the agent run fails — observed during wiki generation / chat with a Gemini-backed provider.Fix
Add
FinishReasonNormalizingHandleras the outermost handler in the agentHttpClientchain:It rewrites the streamed
finish_reasonto the OpenAI SDK's expected set afterLoggingHttpHandler's retry logic has produced the final response, and before deserialization. Values that are already valid (non-Gemini providers) pass through unchanged, so there is no behavioural change for OpenAI/Anthropic/etc.Tests
Adds
tests/OpenDeepWiki.Tests/Agents/FinishReasonNormalizingHandlerTests.cscovering the value mapping and the SSE line transform (32 cases).dotnet testgreen; relies on the existingInternalsVisibleTo("OpenDeepWiki.Tests").Scope
Three files: the new handler, the
AgentFactoryhandler-chain wiring, and the test. No schema/config/API changes.