Skip to content

Conversation

@sydney-runkle
Copy link
Collaborator

@sydney-runkle sydney-runkle commented Oct 24, 2025

  • attach the latest AIMessage to all StructuredOutputErrors so that relevant middleware can use as desired
  • raise StructuredOutputError from ProviderStrategy logic in case of failed parsing (so that we can retry from middleware)
  • added a test suite w/ example custom middleware that retries for tool + provider strategy

Long term, we could add our own opinionated structured output retry middleware, but this at least unblocks folks who want to use custom retry logic in the short term :)

class StructuredOutputRetryMiddleware(AgentMiddleware):
    """Retries model calls when structured output parsing fails."""

    def __init__(self, max_retries: int) -> None:
        self.max_retries = max_retries

    def wrap_model_call(
        self, request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
    ) -> ModelResponse:
        for attempt in range(self.max_retries + 1):
            try:
                return handler(request)
            except StructuredOutputError as exc:
                if attempt == self.max_retries:
                    raise

                ai_content = exc.ai_message.content
                error_message = (
                    f"Your previous response was:\n{ai_content}\n\n"
                    f"Error: {exc}. Please try again with a valid response."
                )
                request.messages.append(HumanMessage(content=error_message))

@github-actions github-actions bot added langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0 feature labels Oct 24, 2025
@sydney-runkle sydney-runkle changed the title feat: structured output retry middleware support structured output retry middleware Oct 24, 2025
@sydney-runkle sydney-runkle changed the title support structured output retry middleware feat: support structured output retry middleware Oct 24, 2025
@github-actions github-actions bot added feature and removed feature labels Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant