← Back to Pull requests

[integrations][plan] Handle finish_reason in OpenAI-family connections #1040

Closed
unknown closed this pull request without merging on Sep 02, 2026 05:02 UTC
View on GitHub
unknown unknown commented on Aug 23, 2026 02:37 UTC
Author

Linked issue: #936

Purpose of change

finish_reason was 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_reason in 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. length and content_filter fail 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_args into outbound messages, and since the chat action replays the assistant response on the next tool-call turn, model_name, promptTokens, completionTokens and structured_output were 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:

  • Java reads through _finishReason().asKnown() rather than finishReason(). The plain accessor throws OpenAIInvalidDataException when the member is absent or null, and VLLMChatModelConnection extends OpenAICompletionsConnection, so OpenAI-compatible servers that omit it would start failing calls that work today.

  • The check runs on the common path, after the model execution is reported and its token metrics are recorded, and before the response is dispatched. A truncated response consumed its full token budget, so that metric is recorded before the rejection. Reporting the model execution as failed instead was considered and rejected: the call itself did not fail, and none of the existing problem categories describes a complete response that is unusable.

  • Java's structured-output result now carries inbound metadata forward instead of starting from an empty map. It previously dropped finish_reason exactly when an output schema was in play, while Python kept it.

  • Three visible behavior changes. A Python caller can no longer forward arbitrary extra_args to 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: ChatResponseEvent was the only route that delivered it, and FAIL and RETRY raise while IGNORE drops the record without sending an event. And a deterministic length is 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 status and incomplete_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 plan module 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_reason is a metadata key rather than a field, and no new exception type or problem category is introduced. Failures reuse ValueError in Python and IllegalStateException in Java, both already used for this class of failure in their packages. Since json.JSONDecodeError subclasses ValueError, Python callers catching today's symptom keep working and simply get a clearer message.

    Documentation

  • doc-needed

  • doc-not-needed

  • doc-included
  • Was this patch authored or co-authored using generative AI tooling?

  • Yes

  • No
  • Generated-by: Claude Code 2.1.234 (Claude Opus 5)

    Diff: 628 lines (+471 / -157) across 7 files Reviewer: weiqingy Review duration: 14544m
    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
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 55.3% (MEDIUM) · Higher = larger blast radius
    55.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 23.9% · Safe to merge
    23.9%