Bug Report: OpenCode Validation Fails on AI SDK V3 Stream Parts #7624

Open
opened 2026-02-16 18:07:46 -05:00 by yindo · 4 comments
Owner

Originally created by @acedergren on GitHub (Jan 26, 2026).

Originally assigned to: @thdxr on GitHub.

Description

OpenCode's Zod validation incorrectly rejects valid AI SDK V3 stream parts from compliant providers, causing validation errors when providers emit finish events according to the official V3 specification.

Error Message

ZodError: [
  {
    "expected": "string",
    "code": "invalid_type",
    "path": ["finish"],
    "message": "Invalid input: expected string, received object"
  }
]

Steps to Reproduce

  1. Use any AI SDK V3-compliant provider (e.g., @acedergren/opencode-oci-genai)
  2. Send a chat message through OpenCode
  3. Observe Zod validation error when provider emits finish event

Expected Behavior

OpenCode should accept AI SDK V3 finish events with the structure:

{
  type: 'finish',
  finishReason: {
    unified: 'stop',  // or 'length', 'content-filter', 'tool-calls', etc.
    raw?: string
  },
  usage: {
    inputTokens: number,
    outputTokens: number
  }
}

Actual Behavior

OpenCode validation expects finish field to be a string, but receives an object containing finishReason (which is correctly an object in V3).

Root Cause

OpenCode's Zod validation schemas appear to be based on AI SDK V2 specification, where finishReason was a simple string. In V3, this was changed to an object with {unified, raw} structure for better standardization across providers.

V2 vs V3 Comparison

AI SDK V2 (legacy):

interface FinishStreamPart {
  type: 'finish';
  finishReason: string;  // Simple string
  usage: UsageInfo;
}

AI SDK V3 (current):

interface FinishStreamPart {
  type: 'finish';
  finishReason: {        // Object with unified format
    unified: string;
    raw?: string;
  };
  usage: UsageInfo;
}

Evidence

  1. Provider declares V3 compliance: The OCI GenAI provider correctly declares specificationVersion = 'v3' and all 726 tests pass
  2. Structure matches official providers: The finish event structure matches Vercel's official providers (OpenAI, Anthropic, xAI)
  3. Conforms to type definitions: Output matches @ai-sdk/provider@3.0.5 type definitions exactly
  4. No "finish" field exists: Code search confirms no "finish" property is emitted - only the correct V3 structure

Impact

  • Users see validation errors in logs when using V3-compliant providers
  • May cause confusion about whether providers are working correctly
  • Blocks adoption of properly implemented V3 providers

Suggested Fix

Update OpenCode's stream part validation schemas to:

  1. Use discriminated unions based on the type field
  2. Accept V3 finish format with finishReason as object:
    const FinishStreamPartSchema = z.object({
      type: z.literal('finish'),
      finishReason: z.object({
        unified: z.string(),
        raw: z.string().optional()
      }),
      usage: UsageInfoSchema
    });
    
  3. Validate based on specificationVersion to support both V2 and V3 providers during transition period

Workaround

The validation error is cosmetic - V3 providers ARE working correctly despite the error. Users can safely ignore this validation error until OpenCode's schemas are updated.

Environment

  • OpenCode version: 1.1.36
  • AI SDK version: @ai-sdk/provider@3.0.5
  • Provider: @acedergren/opencode-oci-genai@0.1.0

Additional Context

The AI SDK V3 migration guide explicitly documents this breaking change from string to object format for finishReason. OpenCode's validation needs to be updated to reflect the current V3 specification.

References:

  • AI SDK V3 Provider Types: @ai-sdk/provider@3.0.5/dist/index.d.ts
  • Official Vercel AI SDK providers (all use V3 object format)

Plugins

None

OpenCode version

1.1.36

Steps to reproduce

Ask to be invited to my private OCI GenAI Provider repo (will be public soon)

Screenshot and/or share link

Image

Operating System

MacOS 26.2

Terminal

Apple terminal Version 2.15 (466)

Originally created by @acedergren on GitHub (Jan 26, 2026). Originally assigned to: @thdxr on GitHub. ## Description OpenCode's Zod validation incorrectly rejects valid AI SDK V3 stream parts from compliant providers, causing validation errors when providers emit finish events according to the official V3 specification. ## Error Message ``` ZodError: [ { "expected": "string", "code": "invalid_type", "path": ["finish"], "message": "Invalid input: expected string, received object" } ] ``` ## Steps to Reproduce 1. Use any AI SDK V3-compliant provider (e.g., `@acedergren/opencode-oci-genai`) 2. Send a chat message through OpenCode 3. Observe Zod validation error when provider emits finish event ## Expected Behavior OpenCode should accept AI SDK V3 finish events with the structure: ```typescript { type: 'finish', finishReason: { unified: 'stop', // or 'length', 'content-filter', 'tool-calls', etc. raw?: string }, usage: { inputTokens: number, outputTokens: number } } ``` ## Actual Behavior OpenCode validation expects `finish` field to be a string, but receives an object containing `finishReason` (which is correctly an object in V3). ## Root Cause OpenCode's Zod validation schemas appear to be based on AI SDK **V2** specification, where `finishReason` was a simple string. In **V3**, this was changed to an object with `{unified, raw}` structure for better standardization across providers. ### V2 vs V3 Comparison **AI SDK V2 (legacy):** ```typescript interface FinishStreamPart { type: 'finish'; finishReason: string; // Simple string usage: UsageInfo; } ``` **AI SDK V3 (current):** ```typescript interface FinishStreamPart { type: 'finish'; finishReason: { // Object with unified format unified: string; raw?: string; }; usage: UsageInfo; } ``` ## Evidence 1. **Provider declares V3 compliance:** The OCI GenAI provider correctly declares `specificationVersion = 'v3'` and all 726 tests pass 2. **Structure matches official providers:** The finish event structure matches Vercel's official providers (OpenAI, Anthropic, xAI) 3. **Conforms to type definitions:** Output matches `@ai-sdk/provider@3.0.5` type definitions exactly 4. **No "finish" field exists:** Code search confirms no "finish" property is emitted - only the correct V3 structure ## Impact - Users see validation errors in logs when using V3-compliant providers - May cause confusion about whether providers are working correctly - Blocks adoption of properly implemented V3 providers ## Suggested Fix Update OpenCode's stream part validation schemas to: 1. **Use discriminated unions** based on the `type` field 2. **Accept V3 finish format** with `finishReason` as object: ```typescript const FinishStreamPartSchema = z.object({ type: z.literal('finish'), finishReason: z.object({ unified: z.string(), raw: z.string().optional() }), usage: UsageInfoSchema }); ``` 3. **Validate based on `specificationVersion`** to support both V2 and V3 providers during transition period ## Workaround The validation error is cosmetic - V3 providers ARE working correctly despite the error. Users can safely ignore this validation error until OpenCode's schemas are updated. ## Environment - OpenCode version: 1.1.36 - AI SDK version: @ai-sdk/provider@3.0.5 - Provider: @acedergren/opencode-oci-genai@0.1.0 ## Additional Context The AI SDK V3 migration guide explicitly documents this breaking change from string to object format for `finishReason`. OpenCode's validation needs to be updated to reflect the current V3 specification. **References:** - AI SDK V3 Provider Types: `@ai-sdk/provider@3.0.5/dist/index.d.ts` - Official Vercel AI SDK providers (all use V3 object format) ### Plugins None ### OpenCode version 1.1.36 ### Steps to reproduce Ask to be invited to my private OCI GenAI Provider repo (will be public soon) ### Screenshot and/or share link <img width="1524" height="1024" alt="Image" src="https://github.com/user-attachments/assets/ea9e69b4-d0b6-4ba1-87c4-d45225edda9e" /> ### Operating System MacOS 26.2 ### Terminal Apple terminal Version 2.15 (466)
Author
Owner

@github-actions[bot] commented on GitHub (Jan 26, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #7439: AIHubMix provider: streaming completes output but errors near end with ZodError (invalid_union: reason/part/delta) - Similar Zod validation error with stream parts
  • #4951: imagen_generate tool crashes with validation error: expected string, received object - Related validation error with expected type mismatches
  • #9020: Claude models fail on MCP tools with no required parameters (undefined vs {}) - Related issue with Zod validation of stream parts
  • #1298: Getting an issue integrating a custom provider: AI_TypeValidationError, failed type validation - Related custom provider validation issues

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 26, 2026): This issue might be a duplicate of existing issues. Please check: - #7439: AIHubMix provider: streaming completes output but errors near end with ZodError (invalid_union: reason/part/delta) - Similar Zod validation error with stream parts - #4951: imagen_generate tool crashes with validation error: expected string, received object - Related validation error with expected type mismatches - #9020: Claude models fail on MCP tools with no required parameters (undefined vs {}) - Related issue with Zod validation of stream parts - #1298: Getting an issue integrating a custom provider: AI_TypeValidationError, failed type validation - Related custom provider validation issues Feel free to ignore if none of these address your specific case.
Author
Owner

@acedergren commented on GitHub (Feb 2, 2026):

After further troubleshooting over concluded its due to lagging support of the ai sdk v6 and language provider v3. Happy to help upgrade the codebase

@acedergren commented on GitHub (Feb 2, 2026): After further troubleshooting over concluded its due to lagging support of the ai sdk v6 and language provider v3. Happy to help upgrade the codebase
Author
Owner

@rekram1-node commented on GitHub (Feb 2, 2026):

As you noticed this is because we are on v5 of the sdk, so not a bug this would be expected if u are using non compliant version.

We will probably do an upgrade soon but it's not just an easy flip there are a lot of third party packages we need to make sure all are v6 compliant too.

@rekram1-node commented on GitHub (Feb 2, 2026): As you noticed this is because we are on v5 of the sdk, so not a bug this would be expected if u are using non compliant version. We will probably do an upgrade soon but it's not just an easy flip there are a lot of third party packages we need to make sure all are v6 compliant too.
Author
Owner

@acedergren commented on GitHub (Feb 2, 2026):

Yes. I'm happy to help when it time . I've adapted my provider to v5 for now.

@acedergren commented on GitHub (Feb 2, 2026): Yes. I'm happy to help when it time . I've adapted my provider to v5 for now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#7624