ai icon indicating copy to clipboard operation
ai copied to clipboard

fix(workers-ai-provider): preserve structured output schema name

Open slegarraga opened this issue 5 months ago • 1 comments

Summary

  • wrap AI SDK structured-output schemas in the OpenAI-compatible json_schema envelope before sending them to Workers AI
  • preserve Output.object({ name, description }) on the request body, defaulting the schema name to response when not provided
  • add REST regression coverage for the request body sent by workers-ai-provider

Fixes #559.

Validation

  • corepack pnpm --filter workers-ai-provider exec vitest --watch=false test/structured-output.test.ts
  • corepack pnpm --filter workers-ai-provider type-check
  • corepack pnpm --filter workers-ai-provider test:ci
  • corepack pnpm exec oxfmt --check packages/workers-ai-provider/src/workersai-chat-language-model.ts packages/workers-ai-provider/test/structured-output.test.ts

slegarraga avatar Jun 10 '26 19:06 slegarraga

🦋 Changeset detected

Latest commit: a406b61c544d582584016b5000ac942499b1f391

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
workers-ai-provider Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jun 10 '26 19:06 changeset-bot[bot]

Thanks for this, @slegarraga — your diagnosis of #559 was correct, and the request-body regression test was a great instinct (we ended up adding similar wire-shape assertions).

We've resolved #559 a different way, so I'm closing this as superseded. Two pieces landed:

  • Partner models (openai/…) — #573 (gateway delegate, in 3.2.0). Third-party catalog slugs now route through AI Gateway and the real @ai-sdk/* provider, which builds the response_format.json_schema.name envelope itself (opt in with createWorkersAI({ binding, providers: [openai] })). So the native code path no longer needs to construct that envelope.
  • Native models (@cf/…) — #576 (in 3.2.0). Here we intentionally did not wrap the schema in { name, schema }: native Workers AI expects a bare JSON Schema under json_schema, and wrapping it risks regressing native structured output (it'd be read as a schema with no type/properties). Instead we fold name → the schema's title and description → description, preserving the guidance while keeping a valid bare schema.

That's the main reason we went this route over the envelope approach in this PR — applying the OpenAI envelope to @cf/... requests would have changed the native wire contract. Combined, the partner failure you hit is fixed by the routing and the native gap is fixed by #576.

Really appreciate you digging in and filing both the issue and the fix. 🙏

threepointone avatar Jun 16 '26 09:06 threepointone