Skip to content

fix(v2): support session-bound generation and text JSON for google - #540

Merged
EyJunge1 merged 1 commit into
tickernelz:v3from
kyue1005:fix/v2-google-gemini-structured-output
Dec 4, 2026
Merged

EyJunge1 merged 1 commit into
tickernelz:v3from
kyue1005:fix/v2-google-gemini-structured-output

Conversation

@kyue1005

@kyue1005 kyue1005 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Problem

When using OpenCode V2 with Google Workspace / Vertex authentication (via opencode-gemini-auth or native Google providers), auto-capture fails on idle sessions with repeated errors:

Auto-capture warning (attempt 1/3): {"phase":"summary","error":"Summary generation failed: The provider response ended unexpectedly."}
Auto-capture final error after 3 attempts: {"phase":"summary","error":"Summary generation failed: The provider response ended unexpectedly."}

Root Cause

  1. Stateless ctx.generate.text 503 on Google Provider:
    In src/v2/legacy-client.ts, ctx.generate.text targets OpenCode's stateless /api/experimental/generate endpoint. Google/Gemini streaming responses close the connection immediately upon completion, causing the server endpoint to abort with 503 Service Unavailable: "The provider response ended unexpectedly.".
    However, OpenCode V2's session-bound generation endpoint (ctx.session.generate({ sessionID, prompt })) manages connection lifecycle within a persistent session context and handles Google SSE streams reliably (200 OK).
  2. OpenCode Session Prompt Payload Schema:
    In OpenCode V2, client.session.prompt requires a top-level text field in addition to parts. Omitting text triggers an InvalidRequestError: Missing key at ["text"].
  3. format: json_schema Loop on Google Providers:
    Similar to Anthropic models (issue Auto capute not working through claude code #278), Google/Gemini providers under OpenCode output markdown JSON blocks rather than OpenAI-style forced JSON schema tool calls. Enforcing format: json_schema can cause the stream to stall or abort.

Solution

  1. Session-Bound Generation in src/v2/legacy-client.ts:
  • create: Creates a lightweight transient capture session via ctx.session.create({ title: "opencode-mem-capture" }).
  • prompt: Tries ctx.session.generate({ sessionID, prompt }) first, falling back to ctx.generate.text if the session is not present.
  • delete: Cleans up the temporary session via ctx.session.remove({ sessionID }).
  1. Text JSON Formatting in src/services/ai/opencode-provider.ts:
  • Extends shouldUseOpencodeTextJson to include google and google/* (alongside anthropic), prompting the model to return a single clean JSON block validated with Zod.
  • Adds text: args.userPrompt to sessionPromptFields and sessionTextJsonPromptFields.
  1. Unit Tests in tests/opencode-provider.test.ts:
  • Adds unit assertions asserting shouldUseOpencodeTextJson("google") and shouldUseOpencodeTextJson("google/gemini-3.8-flash") return true.

Verification

  • Verified build: bun run build succeeds (client & web assets).
  • Verified linter: bun run lint (ESLint & Prettier) passes with 0 warnings/errors.
  • Verified unit test suite: bun test passes (62/62 tests passing).
  • Verified live behavior in OpenCode V2: Auto-capture triggers cleanly on idle sessions with google/gemini-3.8-flash, extracting and indexing technical summaries to Turso without errors.

Prefer real capture sessions with session.generate for providers that abort on the sessionless Generate API, and route google/* through the text-JSON structured-output path.
@EyJunge1
EyJunge1 force-pushed the fix/v2-google-gemini-structured-output branch from 8511263 to fd0601d Compare December 4, 2026 08:55
@EyJunge1 EyJunge1 changed the title fix(v2): support session-bound generation and text JSON for google ge… fix(v2): support session-bound generation and text JSON for google Dec 4, 2026
@EyJunge1
EyJunge1 changed the base branch from main to v3 December 4, 2026 08:55
@EyJunge1
EyJunge1 merged commit 3ecab69 into tickernelz:v3 Dec 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants