Skip to content

[FEATURE] Add try/retry logic to structured_output for self healing #1014

@kaleko

Description

@kaleko

Problem Statement

The structured_output doesn't always work on the first try, and throws an exception when there are validation errors (especially with more complicated output schemas). I've found that if you try/retry once, and provide the agent the exception from the first iteration, it will self heal appropriately. It would be nice if this were built-in to the structured_output function, e.g. something like structured_output(max_retries=3)

Proposed Solution

Here is some sample code which works:

  # first I have the agent do some analysis, then I do:
  try:
        result = agent.structured_output(
            OutputSchemaModel,
            "Based on your prior analysis, generate a structured OutputSchemaModel output.",
        )
    except Exception as e:
        result = agent.structured_output(
            OutputSchemaModel,
            f"Try again to generate a structured OutputSchemaModel output. Your previous attempt failed with this exception: {e}",
        )
    return result

Use Case

Use case for people who want reliable structured output with reduced probability of the structured_output function throwing an exception when it fails.

Alternatives Solutions

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions