Skip to content

Error converting zod schema to valid json schema #194

Open
@li-dennis

Description

@li-dennis

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions