Skip to content

feat(helpers/zod): add zodRealtimeFunction for Realtime API tool definitions#1864

Open
xodn348 wants to merge 1 commit intoopenai:masterfrom
xodn348:fix/zod-realtime-function
Open

feat(helpers/zod): add zodRealtimeFunction for Realtime API tool definitions#1864
xodn348 wants to merge 1 commit intoopenai:masterfrom
xodn348:fix/zod-realtime-function

Conversation

@xodn348
Copy link
Copy Markdown

@xodn348 xodn348 commented May 3, 2026

Summary

The Realtime API's RealtimeFunctionTool interface does not include a strict field, unlike the Responses API's function tool type. As a result, zodResponsesFunction cannot be used to build Realtime API tool definitions — it emits strict: true, which is an unrecognized field for that API endpoint and silently breaks schema validation.

This PR adds zodRealtimeFunction, a new exported helper in src/helpers/zod.ts that mirrors zodResponsesFunction but targets RealtimeFunctionTool. It converts a Zod v3 or v4 schema into the correct tool shape (with type, name, parameters, and an optional description) and omits strict. Callers pass the Zod object for type safety during tool definition; they parse the arguments themselves when the model calls the tool (e.g., MyParams.parse(JSON.parse(event.arguments))).

Issue

Fixes #1809

Local verification

Installed deps with yarn install --frozen-lockfile --ignore-scripts (skipping the git-swap postinstall), ran typecheck and the targeted test suite:

$ npx tsc --noEmit --project tsconfig.json 2>&1 | grep -v '^examples/'
(no output — zero src/ or tests/ errors)

$ npx jest tests/helpers/zod.test.ts --no-coverage
PASS tests/helpers/zod.test.ts
  zodResponseFormat (Zod v3)
    ✓ does the thing (5 ms)
    ✓ automatically adds optional properties to `required` (2 ms)
    ✓ automatically adds properties with defaults to `required` (1 ms)
    ✓ allows description field to be passed in (1 ms)
    ✓ kitchen sink types (2 ms)
    ✓ throws error on optional fields (1 ms)
    ✓ throws error on nested optional fields (1 ms)
    ✓ does not warn on union nullable fields (1 ms)
  zodResponseFormat (Zod v4)
    ✓ does the thing (3 ms)
    ✓ automatically adds optional properties to `required` (1 ms)
    ✓ automatically adds properties with defaults to `required` (2 ms)
    ✓ allows description field to be passed in
    ✓ kitchen sink types (2 ms)
    ✓ throws error on optional fields (1 ms)
    ✓ throws error on nested optional fields (1 ms)
    ✓ does not warn on union nullable fields (1 ms)
  zodRealtimeFunction (Zod v3)
    ✓ builds a RealtimeFunctionTool without strict (1 ms)
    ✓ omits description when not provided (1 ms)
  zodRealtimeFunction (Zod v4)
    ✓ builds a RealtimeFunctionTool without strict (1 ms)
    ✓ omits description when not provided

Tests: 20 passed, 20 total
=== LOCAL_TEST_PASSED ===

Risk

The change is additive — no existing function is modified. zodRealtimeFunction is a new export in src/helpers/zod.ts, which the generator is documented to never overwrite. Tests for existing helpers pass unchanged.

…nitions

zodResponsesFunction sets strict: true, which the Realtime API's
RealtimeFunctionTool interface does not support. zodRealtimeFunction
is a parallel helper that converts a Zod schema to a RealtimeFunctionTool
without the strict field, letting callers define Realtime API function
tools with the same convenience as zodResponsesFunction.

Closes openai#1809
@xodn348 xodn348 requested a review from a team as a code owner May 3, 2026 07:30
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.

zod helpder for RealtimeFunctionTool

1 participant