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

Provide a safeparse / way to access the result in case of zod schema validation error #3451

Open
williamlmao opened this issue Nov 1, 2024 · 1 comment
Labels
ai/core enhancement New feature or request

Comments

@williamlmao
Copy link

Feature Description

Provide a safeparse / way to access the result in case of zod schema validation error.

Use Case

Sometimes we get a response that fails zod validation but is perfectly usable. In these cases, we'd rather not throw a validation error and instead would prefer to continue.

Additional context

No response

@lgrammel lgrammel added enhancement New feature or request ai/core labels Nov 4, 2024
@williamlmao
Copy link
Author

Wanted to add some additional context here. In the example below, the LLM gets it super close, but it just added a bit of text that causes the enum parse to fail. This is fairly common and we can expect / fix this, but we just need a chance to do it before the entire thing throws an error.

The big problem with this throwing an error is now we cannot access the full response from the LLM provider. We cannot track usage data on this request which was technically successful.

We are able to grab the output value and handle this, but it's not very ergonomic since now this has to be handled/returned from a catch block.

In the example below, LLM gives "Wisdom (Insight) Check" when it should have given "Wisdom Check" (we can fix this particular issue with some better instruction, but still think we need a way to access the original response from the LLM provider / have a safeParse option)

  "error": {
    "cause": {
      "issues": [
        {
          "code": "invalid_enum_value",
          "options": [
            "Strength Check",
            "Dexterity Check",
            "Constitution Check",
            "Intelligence Check",
            "Wisdom Check",
            "Charisma Check",
            "Strength Saving Throw",
            "Dexterity Saving Throw",
            "Constitution Saving Throw",
            "Intelligence Saving Throw",
            "Wisdom Saving Throw",
            "Charisma Saving Throw",
            "Athletics Check",
            "Acrobatics Check",
            "Sleight of Hand Check",
            "Stealth Check",
            "Initiative Check",
            "Arcana Check",
            "History Check",
            "Investigation Check",
            "Nature Check",
            "Religion Check",
            "Animal Handling Check",
            "Insight Check",
            "Medicine Check",
            "Perception Check",
            "Survival Check",
            "Deception Check",
            "Intimidation Check",
            "Performance Check",
            "Persuasion Check",
            "Attack",
            "Spell Attack"
          ],
          "path": [
            "rolls",
            0,
            "roll"
          ],
          "message": "Invalid enum value. Expected 'Strength Check' | 'Dexterity Check' | 'Constitution Check' | 'Intelligence Check' | 'Wisdom Check' | 'Charisma Check' | 'Strength Saving Throw' | 'Dexterity Saving Throw' | 'Constitution Saving Throw' | 'Intelligence Saving Throw' | 'Wisdom Saving Throw' | 'Charisma Saving Throw' | 'Athletics Check' | 'Acrobatics Check' | 'Sleight of Hand Check' | 'Stealth Check' | 'Initiative Check' | 'Arcana Check' | 'History Check' | 'Investigation Check' | 'Nature Check' | 'Religion Check' | 'Animal Handling Check' | 'Insight Check' | 'Medicine Check' | 'Perception Check' | 'Survival Check' | 'Deception Check' | 'Intimidation Check' | 'Performance Check' | 'Persuasion Check' | 'Attack' | 'Spell Attack', received 'Wisdom (Insight) Check'",
          "received": "Wisdom (Insight) Check"
        }
      ],
      "name": "ZodError"
    },
    "value": {
      "narration": "Master Armand attempts to sense the true nature of the chalice and the shadows' intentions.",
      "rolls": [
        {
          "characterId": 6570,
          "roll": "Wisdom (Insight) Check",
          "vantage": "none",
          "dc": 0
        }
      ]
    },
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants