[integrations][plan] Handle finish_reason in OpenAI-family connections #1040
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.55) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.55 · 628 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.386
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #1040"
general
weight: 0.35
weiqingy
Linked issue: #936
Purpose of change
finish_reasonwas never read in either language, so a truncated completion came back as ordinary partial content. Under structured output that content went straight to a JSON parser, so the caller got an error naming the parser instead of the truncation. Worse, when the truncated content happened to still be valid JSON, nothing raised at all and the caller silently received wrong data.Cases 1 and 3 of #936 shipped in #952 and #989. This is the last one.
The change has three parts:
1. Both languages record the provider's reason as
finish_reasonin message metadata. Unknown values are stored verbatim, and an absent reason writes no key.2. The shared chat-response path checks it once per response, before that response is dispatched as text, structured output, or tool calls.
lengthandcontent_filterfail with the cause named.stop,tool_calls, unknown values and an absent key proceed as before.3. Python's outbound converter had to be fixed first. It merged all of
extra_argsinto outbound messages, and since the chat action replays the assistant response on the next tool-call turn,model_name,promptTokens,completionTokensandstructured_outputwere reaching the provider as message fields. Each role now sends only the fields OpenAI defines, matching Java.Four things worth knowing when reading the diff:
_finishReason().asKnown()rather thanfinishReason(). The plain accessor throwsOpenAIInvalidDataExceptionwhen the member is absent or null, andVLLMChatModelConnectionextendsOpenAICompletionsConnection, so OpenAI-compatible servers that omit it would start failing calls that work today.finish_reasonexactly when an output schema was in play, while Python kept it.extra_argsto the provider, which Java never allowed and nothing in the repo relies on. A truncated or filtered response no longer reaches the caller under any error handling strategy:ChatResponseEventwas the only route that delivered it, andFAILandRETRYraise whileIGNOREdrops the record without sending an event. And a deterministiclengthis now retried on every call rather than only on structured ones, a cost that repeats for each candidate in a model route.Out of scope: the Responses API reports
statusandincomplete_details.reason, a different vocabulary. Providers that record no reason are unaffected and pick the behavior up if they later write the same key.Tests
Verified per commit against the modules each one touches: the
planmodule at 301 Java tests with 0 failures, and the full non-integration Python suite at 942 passed with 13 skipped. Spotless and Ruff are clean. CI covers the full build and the version matrix.Coverage was driven by mutation testing, and every mutant in scope is killed in both languages: swapping in the throwing accessor, moving either capture inside the token-usage guard, dropping either terminal reason, reinstating the blanket merge, neutering the gate call, and widening the gate to reject an accepted reason. Acceptance and rejection are pinned by disjoint tests, so neither can mask the other.
Two test properties are load-bearing and easy to lose in a later refactor. The throwing tests use valid JSON, because truncated JSON raises on its own and a malformed fixture would pass with no gate at all. And one test asserts that a rejected response produces no parser execution report, which is what pins the gate ahead of the structured-output wrapper rather than inside it.
Both languages were also driven through identical inputs to confirm they agree, including that neither sends the metadata keys to a provider.
API
No public API change.
finish_reasonis a metadata key rather than a field, and no new exception type or problem category is introduced. Failures reuseValueErrorin Python andIllegalStateExceptionin Java, both already used for this class of failure in their packages. Sincejson.JSONDecodeErrorsubclassesValueError, Python callers catching today's symptom keep working and simply get a clearer message.Documentation
doc-neededdoc-not-neededdoc-includedWas this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code 2.1.234 (Claude Opus 5)