Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error converting zod schema to valid json schema #194

Open
li-dennis opened this issue Jan 14, 2025 · 1 comment
Open

Error converting zod schema to valid json schema #194

li-dennis opened this issue Jan 14, 2025 · 1 comment

Comments

@li-dennis
Copy link

li-dennis commented Jan 14, 2025

I have a fairly complex zod schema, for better or worse, and am noticing that instructor converts it to schemas with nonexistent definitions, causing it to fail json schema meta validation

where are the zod types converted to json schemas? happy to fix this just don't know where to look :)

here's a toy example of the issue

import { z } from "zod";

const Type = z.enum(["A", "B", "C"]);

const BaseItem = z.object({
  foo: z.string(),
  type: Type,
});

const Schema = z.object({
  a: z.array(BaseItem),
  b: z.array(BaseItem)
});

Generated JSON Schema

{
  "type": "object",
  "properties": {
    "a": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "foo": { "type": "string" },
          "type": {
            "type": "string",
            "enum": ["A", "B", "C"]
          }
        },
        "required": ["foo", "type"]
      }
    },
    "b": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "foo": { "type": "string" },
          "type": {
            "$ref": "#/definitions/Root/properties/a/items/properties/type"
          }
        },
        "required": ["foo", "type"]
      }
    }
  },
  "required": ["a", "b"]
}

Notably, it's generating a reference to a definition within a top level Root which doesn't exist. this then causes the tool calling to fail with

tools.0.input_schema: JSON schema is invalid - please consult https://json-schema.org or our documentation at https://docs.anthropic.com/en/docs/tool-use
@li-dennis
Copy link
Author

li-dennis commented Jan 15, 2025

I see it's handled in zod-stream via https://github.com/hack-dance/island-ai/blob/main/public-packages/zod-stream/src/response-model.ts#L31-L34

It seems like a simple fix here would be to disable these sorts of json schema refs via

  const { definitions } = zodToJsonSchema(schema, {
    name: safeName,
    errorMessages: true,
   $refStrategy: "none", // added to disable refs
  })

But in general though, it would be nice if I could control the generation of the json schema when calling instructorjs. eg although this library doesn't support google's gemini vector ai api, that api does have tool calling support with a limited subset of json schema. and in this case, i believe the issue is due to a change in anthropic's api..

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

No branches or pull requests

1 participant