Skip to content

provider: 413 payload-too-large surfaces as Upstream response was not valid JSON and never triggers compaction #59067

Closed as duplicate

Description

@fahrim

Summary

A request rejected by the provider with HTTP 413 (payload too large) is surfaced as the misleading provider.invalid-request: Upstream request failed: [server_error] Upstream response was not valid JSON, the session is marked failed, and no automatic compaction or retry happens — even though the compaction docs promise one recovery when a provider rejects a request as too long. Every later prompt fails identically until a manual /compact.

Environment

  • opencode version: 2.1.4
  • OS: Windows 10.0.26200 (win32 x64)
  • Terminal: TERM_PROGRAM=vscode, COLORTERM=truecolor
  • Shell: cmd.exe
  • Install/channel: latest (npm global @opencode/cli)
  • Active plugins: none
  • Provider/model: opencode-go/deepseek-v4.1-flash

Reproduction

  1. Run a long session that inspects screenshots: ~180 tool results, each a read of an emulator PNG, returned as base64 and kept in history.
  2. Send any prompt. The outgoing body reaches ~17 MB because every previous image result is re-sent.
  3. The provider rejects it with 413. OpenCode reports:
    provider.invalid-request
    Upstream request failed: [server_error] Upstream response was not valid JSON
    status: 413
    body: {"error":{"type":"server_error","code":"server_error","message":"Upstream request failed: [server_error] Upstream response was not valid JSON"}}
    
  4. Send another prompt (a one-word devam et). Same failure. The session is stuck.

Diagnostics for that session:

Metric Value
GET /api/session/<id>/context response 17,086,630 bytes
Content inside tool results 11,579,996 chars
Tool results in context 182
Messages / assistant tool calls 1,307 / 1,152
Session outcome failed

Expected Behavior

  • A 413 / payload too large rejection should be classified as an oversize request, not as an invalid upstream response, and should trigger the documented single recovery (compact, then retry once).
  • If recovery is impossible, the error should say the request was too large and suggest compaction, instead of implying the provider returned malformed JSON.
  • Compaction thresholds should account for request bytes, not only tokens: here the conversation was nowhere near the token limit (~40k tokens after the previous compaction) yet exceeded the gateway's body limit, so token-based buffer/keep never fired.

Actual Behavior

  • The provider's raw body text is passed through as the error message, which points at the wrong layer (JSON parsing) and hides the real cause (body size).
  • No compaction attempt and no retry; the session becomes permanently unusable because every subsequent prompt resends the same oversized history.
  • Session outcome is failed with no hint that compaction would fix it.

Additional Context

  • Workaround that resolves it completely:
    opencode api post /api/session/<id>/compact --data '{}'
    Context dropped from 17.1 MB to 160 KB and the session outcome flipped from failed to succeeded; the same session continued normally afterwards.
  • GET /api/session/<id>/context was the only way to see the real problem (response size, and that 11.6 MB of it lived in tool results). A size warning in the UI or an entry in /api/session would have made this diagnosable without guesswork.
  • Repro is deterministic for this session (100%), and presumably for any session that accumulates enough image tool results.
  • Related but distinct: read on a PNG embeds base64 into history permanently, so the request grows monotonically. Even with compaction working, some guidance or a per-result size cap would help.

Activity

  1. opencode-agent commented on Oct 25, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report and the diagnostics. I think this is a duplicate of #53107. In v2, an HTTP 413 is already classified as a payload-too-large error, which is why you see provider.invalid-request. But the automatic compact-and-retry only runs for errors classified as context overflow, so a 413 never triggers it. Every later prompt then resends the same oversized history. The "Upstream response was not valid JSON" text is the message the OpenCode Go gateway sent back with the 413, not a JSON parsing problem in OpenCode.

    Possible duplicates and closely related issues, ordered from most to least likely:

    Until that's fixed, compacting the session manually (/compact, or POST /api/session/<id>/compact as you did) is the workaround.

    The bot will close this issue as a duplicate in 1 day unless you reply explaining how your problem is different.

  2. opencode-agent commented on Oct 26, 2026

    @opencode-agent
    Contributor

    We haven't heard back since this was flagged as a likely duplicate, so I'm closing it as a duplicate of #53107. Both issues have the same cause: an HTTP 413 is classified as payload-too-large, but automatic compact-and-retry only runs for context-overflow errors. Please follow #53107 for updates (its fix PR is #53108). Your request for byte-based compaction thresholds is covered in #48095.

    Until it's fixed, compacting manually (/compact, or POST /api/session/<id>/compact) gets the session working again. If your problem turns out to be different, reply here and we can reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions