Repository navigation
Description
Description
When using the Kimi k2.6 model provider, opencode fails with the error:
Error from provider: JSON Schema not supported: could not understand the instance `{'_def': ...}`
The tool schemas sent to the API contain raw Zod internal properties (_def, typeName, ~standard, _cached) instead of standard JSON Schema.
Environment
- opencode version: v1.0.146 (via context-mode plugin)
- Model: Kimi k2.6
- OS: Linux
Root Cause
The ctx_batch_execute (and likely other) MCP tools define their parameters using Zod schemas. When opencode serializes these schemas for the provider API, the Zod internal representation leaks through instead of being converted to clean JSON Schema.
The failing schema excerpt:
{
"_def": {
"type": {
"_def": {
"unknownKeys": "strip",
"catchall": {"_def": {"typeName": "ZodNever"}, "~standard": {"version": 1, "vendor": "zod"}},
"typeName": "ZodObject"
},
"~standard": {"version": 1, "vendor": "zod"},
"_cached": null
},
"minLength": {"value": 1},
"typeName": "ZodArray",
"description": "Commands to execute as a batch..."
},
"~standard": {"version": 1, "vendor": "zod"}
}Note typeName: "ZodArray", typeName: "ZodObject", _def, ~standard — all Zod internals.
What the provider expects (standard JSON Schema):
{
"type": "array",
"items": {
"type": "object",
"properties": {...}
},
"minItems": 1,
"description": "Commands to execute as a batch..."
}Likely the conversion uses JSON.parse(JSON.stringify(schema)) or similar shallow serialization that doesn't invoke Zod's toJSON() or zod-to-json-schema properly.
Steps to Reproduce
- Run opencode with Kimi k2.6 as the provider
- The provider layer attempts to serialize MCP tool schemas to send to the API
- Zod internals leak through, Kimi API rejects with "JSON Schema not supported"
Expected Behavior
Tool schemas should be serialized to standard JSON Schema (draft-07 or similar) before being sent to the provider API.
Suggested Fix
When serializing Zod schemas for provider tool definitions, use a proper conversion library like zod-to-json-schema or at minimum strip _def, ~standard, _cached, and typeName internal properties before sending. The serialization should produce output compatible with JSON Schema draft-07.
Workaround candidate for the provider layer:
function sanitizeSchema(schema: unknown): unknown {
if (schema && typeof schema === 'object' && '_def' in (schema as any)) {
return sanitizeSchema((schema as any)._def);
}
if (Array.isArray(schema)) {
return schema.map(sanitizeSchema);
}
if (schema && typeof schema === 'object') {
const cleaned: Record<string, unknown> = {};
for (const [key, value] of Object.entries(schema)) {
if (key === '_def' || key === '~standard' || key === '_cached') continue;
cleaned[key] = sanitizeSchema(value);
}
return cleaned;
}
return schema;
}Plugins
context-mode
OpenCode version
No response
Steps to reproduce
No response
Screenshot and/or share link
No response
Operating System
Windows - WSL - Debian
Terminal
No response
Activity
github-actions commentedon May 21, 2026
This issue might be a duplicate of existing issues. Please check:
zodToJsonSchemafromzod-to-json-schemais incompatible with Zod v4 and leaks Zod internals instead of producing valid JSON Schema output_zod.def, etc.) to bleed through schema serializationIf your issue is distinct (e.g., specific to the MCP
ctx_batch_executetool or the Kimi k2.6 provider path rather than the general serialization layer), please add that context to help distinguish it from the above.Baijack-star commentedon May 21, 2026
I took a quick look at current
mainto separate this from the older general Zod v3/v4 reports.The likely useful split is where the raw Zod object first enters the tool pipeline, not the Kimi provider adapter itself:
packages/opencode/src/tool/json-schema.ts(ToolJsonSchema.fromTool()/ EffectSchema.toJsonSchemaDocument).packages/opencode/src/tool/registry.tsviaz.toJSONSchema(...)andnormalizeZodJsonSchema(...).packages/opencode/src/session/tools.tswithasSchema(item.inputSchema).jsonSchema, then passed throughProviderTransform.schema(...).packages/opencode/src/provider/transform.tsalready has a Kimi/Moonshot sanitizer, but it assumes the input is already JSON Schema. It strips/adjusts$refsiblings and tupleitems; it does not and probably should not try to interpret raw Zod internals.So I would add the regression/guard one layer earlier: right before
jsonSchema(transformed)insession/tools.tsand for plugin tools after registry load, assert that the final schema tree contains none of_def,def,_zod,~standard,_cached, ortypeName. If that guard fails, log the tool name/source (ctx_batch_execute/ context-mode) and fail with a local diagnostic before the provider request is built.That should answer whether this is still the known Zod conversion bug or a distinct bypass in the context-mode/MCP tool path. For Kimi specifically, a focused failing test would create a synthetic tool with an array-of-object parameter, run it through
SessionTools.resolveforproviderID: "moonshotai"or a Kimi model id, and assert the final AI SDK tool schema is:{ "type": "array", "items": { "type": "object", "properties": {} } }with no Zod-private keys anywhere in the serialized body.
nibirrayy commentedon Jun 1, 2026
Same issue on my setup — additional context:
Environment:
~/.opencode/bin/opencode)opencode-go/kimi-k2.6, endpointhttps://opencode.ai/zen/go/v1/chat/completions,@ai-sdk/openai-compatible)"plugin": ["context-mode"])Error:
What I've confirmed:
_def,typeName: "ZodObject",~standard: {version: 1, vendor: "zod"}) is being sent directly to the OpenCode Go endpoint without conversion to standard JSON Schema@ai-sdk/anthropic(MiniMax M2.7, Qwen) work fine — the issue is specific to models routed through@ai-sdk/openai-compatiblethat have strict JSON Schema validation (Kimi)@ai-sdk/anthropic, GPT, Gemini) work fineConfig snippet (
opencode.json):{ "plugin": ["context-mode"], "mcp": { "context-mode": { "type": "local", "command": ["context-mode"] } } }Impact: Kimi K2.6 is completely unusable with opencode when any tools are registered via the OpenCode Go endpoint. Would love to see #28997 or #28080 land — happy to test any fixes.
giomartinsdev commentedon Jun 5, 2026
i have this issue too
fix: advertised tool schema leaking Zod properties
github-actions commentedon Aug 5, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.