Repository navigation
fix(runtime): preserve anyOf/oneOf unions in frontend tool schemas - #6143
Merged
Merged
Conversation
…ol schemas
A frontend/client tool (`useFrontendTool`) whose Zod `parameters` use
`z.discriminatedUnion(...)` — or any schema that serializes to a JSON-schema
`anyOf`/`oneOf` node — silently lost the union-typed field when calling OpenAI.
The tool call arrived with that field missing or empty.
Root cause: the runtime has two JSON-Schema -> Zod converters. The shared one
(`@copilotkit/shared`) already handles `anyOf`/`oneOf` as `z.union`, but the
local copy in `packages/runtime/src/agent/index.ts` — used by the classic
`BuiltInAgent` / AI SDK path via `convertToolsToVercelAITools` — never did.
A union node carries no top-level `type`, so it hit the empty-schema guard and
collapsed to `z.object({})`. The reconstructed tool schema therefore dropped
the union entirely (most visibly for a union nested inside array `items`), so
the model was never offered those fields and could not emit them.
Fix: handle `anyOf`/`oneOf` as `z.union` before the empty-schema guard,
mirroring the shared converter. A single-variant union unwraps to that variant.
Backward compatible: only previously-broken union nodes change behavior
(empty object -> real union). Empty `{}` schemas, typed nodes, and the
`isJsonSchema` gate are untouched.
Repro: a `useFrontendTool` with `parameters: z.object({ blocks: z.array(
z.discriminatedUnion("type", [...])) })` against OpenAI — before, `blocks`
items arrived empty; after, both variants survive into the model call.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
davidmckayv
requested review from
jpr5,
marthakelly,
mme,
ranst91 and
tylerslaton
as code owners
July 23, 2026 21:58
@copilotkit/a2ui-renderer
@copilotkit/agentcore-runner
@copilotkit/angular
@copilotkit/channels
@copilotkit/channels-core
@copilotkit/channels-discord
@copilotkit/channels-intelligence
@copilotkit/channels-slack
@copilotkit/channels-teams
@copilotkit/channels-telegram
@copilotkit/channels-ui
@copilotkit/channels-whatsapp
@copilotkit/core
@copilotkit/react-core
@copilotkit/react-native
@copilotkit/react-textarea
@copilotkit/react-ui
@copilotkit/runtime
@copilotkit/runtime-client-gql
@copilotkit/sdk-js
@copilotkit/shared
@copilotkit/sqlite-runner
@copilotkit/voice
@copilotkit/vue
@copilotkit/web-components
@copilotkit/web-inspector
commit: |
Contributor
|
Size Change: 0 B Total Size: 1.19 MB ℹ️ View Unchanged
|
…_def.typeName _def.typeName isn't on ZodTypeDef's public type (TS2339 in check-types); instanceof is the type-safe equivalent. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
tylerslaton
approved these changes
Jul 24, 2026
samjulien
added a commit
that referenced
this pull request
Jul 28, 2026
## What Expands the React Native docs page from a "get to first chat" quickstart into a full production guide, matching the depth of the Slack/Teams frontend pages. The existing quickstart Steps are unchanged; the new material is added after them. ### Production sections added - **Headless imports** — `@copilotkit/react-native/headless` for a custom-UI app, avoiding the chat/attachment native peer deps the default barrel re-exports. - **Metro configuration for release bundles** — `unstable_enablePackageExports` + browser-first `unstable_conditionNames` so the transitive `jose` dependency resolves to its browser build (fails only at release-bundle time otherwise). - **Polyfills** — documents the real auto-installed barrel + granular subpaths, including the `crypto` polyfill (backed by `Math.random`) and the recommendation to install a secure RNG for production. - **Provider options** — the full `CopilotKitProvider` prop surface: `runtimeUrl`, `headers` (auth), `credentials`, `properties`, `onError`, `debug`, `defaultThrottleMs`, `useSingleEndpoint`. - **Runtime and model wiring** — the string model form, `OPENAI_BASE_URL` (and Anthropic/Google equivalents) for OpenAI-compatible endpoints, `createCopilotNodeListener` zero-dep serving, and `BuiltInAgent` options. - **Connecting from a device or bench** — LAN IP, Android emulator `10.0.2.2`, `adb reverse` for USB, and Android release cleartext HTTP via `expo-build-properties`. - **Frontend tools and generative UI** — `useFrontendTool` with zod/Standard Schema, the send-a-turn recipe, `useAgent` read-only subscribe + `threadId`, discriminated-union params, and variable-length `ScrollView` rendering. - **Run lifecycle and UI** — `isRunning` outliving the visible compose, and gating overlays/reveal motion accordingly. - **Voice and speech-to-text** — DIY STT with a graceful-fallback warning for de-Googled/automotive hardware. - Refreshed **What's included** and **Known limitations**, plus a **Next steps** section. ### Dependency on runtime / react-native fixes Three of the four fixes this guide relies on are now **merged to `main`**, so it documents them as current behavior: - `@copilotkit/react-native/headless` subpath (headless provider + hooks without chat/attachment deps) — #6142 ✅ merged - `resolveModel` honoring `OPENAI_BASE_URL` / `ANTHROPIC_BASE_URL` / `GOOGLE_GENERATIVE_AI_BASE_URL` for the string model form — #6140 ✅ merged - union / `anyOf` / `oneOf` tool-param schemas surviving JSON-schema→zod conversion — #6143 ✅ merged The fourth — `useAgent` accepting an optional `threadId` (#6141) — is **still open**. The guide therefore does **not** document it as working; it is listed under **Known limitations** instead, and will be promoted once #6141 lands. Every API, export, env var, and behavior in the page was independently re-verified against the package source (three parallel source audits over `packages/{react-native,react-core,runtime}`). That pass turned up one overstatement, fixed in 6eefe2b: only `/headless` and the root barrel side-effect-import the polyfill barrel — `@copilotkit/react-native/components` does not, so the "all three surfaces auto-install" wording was corrected. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A frontend/client tool (
useFrontendTool) whose Zodparametersusez.discriminatedUnion(...)— or any schema that serializes to a JSON-schemaanyOf/oneOfnode — silently loses the union-typed field when calling OpenAI. The tool call arrives with that field missing or empty. Switching the same tool to a flat object with an enum discriminant works, which points at schema conversion rather than the model.Root cause
The runtime has two JSON-Schema → Zod converters:
@copilotkit/shared'sconvertJsonSchemaToZodSchemaalready handlesanyOf/oneOfasz.union(and$ref, null-unions, graceful fallback).packages/runtime/src/agent/index.ts— used by the classicBuiltInAgent/ AI SDK path viaconvertToolsToVercelAITools— never did.A union node carries no top-level
type, so it hit the empty-schema guard (if (!jsonSchema.type)) and collapsed toz.object({}). The reconstructed tool schema therefore dropped the union entirely — most visibly for a union nested inside arrayitems— so the model was never offered those fields and could not emit them. (The legacy GraphQL OpenAI adapter is unaffected: it forwards the JSON schema directly.)Fix
Handle
anyOf/oneOfasz.unionbefore the empty-schema guard, mirroring the already-proven shared converter. A single-variant union unwraps to that variant.Backward compatibility
Only previously-broken union nodes change behavior (empty object → real union). Empty
{}schemas, typed nodes, and theisJsonSchemagate are untouched. Two regression tests added: a directanyOfconversion and the exact nested-itemsoneOftrap.Repro
A
useFrontendToolwithagainst OpenAI: before,
blocksitems arrived empty; after, both variants survive into the model call.Note on OpenAI strict mode
This path does not enable OpenAI strict function-calling, so once the union survives conversion it serializes back to
anyOfand OpenAI accepts it. The data loss was upstream, in CopilotKit's own converter, not an OpenAI strict-mode limitation.🤖 Generated with Claude Code