magentic icon indicating copy to clipboard operation
magentic copied to clipboard

How do you skip errors such as StructuredOutputError?

Open Lawouach opened this issue 1 year ago • 1 comments

Hi,

It seems to me that when an error such as StructuredOutputError is raised, the whole process terminates. Is there a way to make it more forgiving? I'd personally wouldn't mind if a few retrieval failed during streaming if I could keep these that succeeded.

Am I looking at this the wrong way?

Lawouach avatar May 14 '24 12:05 Lawouach

Some tips on dealing with StructuredOutputError from https://magentic.dev/structured-outputs/#json-schema

If a StructuredOutputError is raised often, this indicates that the LLM is failing to match the schema. The traceback for these errors includes the underlying pydantic ValidationError which shows in what way the received response was invalid. To combat these errors there are several options

  • Add descriptions or examples for individual fields to demonstrate valid values.
  • Simplify the output schema, including using more flexible types (e.g. str instead of datetime) or allowing fields to be nullable with | None.
  • Switch to a "more intelligent" LLM. See Configuration for how to do this.

You could also retry the request using https://github.com/jd/tenacity or https://github.com/hynek/stamina . This is less effective for @prompt_chain if it is failing at some intermediate step.

There's an open issue for having the LLM adjust its output based on the error https://github.com/jackmpcollins/magentic/issues/166

If none of the above helps, could you please share some example code to recreate the issue. Thanks

jackmpcollins avatar May 15 '24 06:05 jackmpcollins