test_structured_output_retry.py — langchain Source File
Architecture documentation for test_structured_output_retry.py, a python file in the langchain codebase. 10 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR dd58b626_28b2_b72e_0314_162a5a760201["test_structured_output_retry.py"] 2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"] dd58b626_28b2_b72e_0314_162a5a760201 --> 2bf6d401_816d_d011_3b05_a6114f55ff58 f69d6389_263d_68a4_7fbf_f14c0602a9ba["pytest"] dd58b626_28b2_b72e_0314_162a5a760201 --> f69d6389_263d_68a4_7fbf_f14c0602a9ba 9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"] dd58b626_28b2_b72e_0314_162a5a760201 --> 9444498b_8066_55c7_b3a2_1d90c4162a32 121262a1_0bd6_d637_bce3_307ab6b3ecd4["langchain_core.tools"] dd58b626_28b2_b72e_0314_162a5a760201 --> 121262a1_0bd6_d637_bce3_307ab6b3ecd4 2ff5ef5d_5050_1ab2_e4f2_f72e391945a2["langgraph.checkpoint.memory"] dd58b626_28b2_b72e_0314_162a5a760201 --> 2ff5ef5d_5050_1ab2_e4f2_f72e391945a2 dd5e7909_a646_84f1_497b_cae69735550e["pydantic"] dd58b626_28b2_b72e_0314_162a5a760201 --> dd5e7909_a646_84f1_497b_cae69735550e d9a6942a_c37a_07f8_ed13_74d0fdc117be["langchain.agents"] dd58b626_28b2_b72e_0314_162a5a760201 --> d9a6942a_c37a_07f8_ed13_74d0fdc117be a681398d_ed44_c914_1a44_5d174223b069["langchain.agents.middleware.types"] dd58b626_28b2_b72e_0314_162a5a760201 --> a681398d_ed44_c914_1a44_5d174223b069 6a1fbcf5_4f70_081d_b76c_92eaad743465["langchain.agents.structured_output"] dd58b626_28b2_b72e_0314_162a5a760201 --> 6a1fbcf5_4f70_081d_b76c_92eaad743465 069947d2_727b_035a_0691_c12203e2f5a6["tests.unit_tests.agents.model"] dd58b626_28b2_b72e_0314_162a5a760201 --> 069947d2_727b_035a_0691_c12203e2f5a6 style dd58b626_28b2_b72e_0314_162a5a760201 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Tests for StructuredOutputRetryMiddleware functionality."""
from collections.abc import Callable
import pytest
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from pydantic import BaseModel
from langchain.agents import create_agent
from langchain.agents.middleware.types import (
AgentMiddleware,
ModelRequest,
ModelResponse,
)
from langchain.agents.structured_output import StructuredOutputError, ToolStrategy
from tests.unit_tests.agents.model import FakeToolCallingModel
class StructuredOutputRetryMiddleware(AgentMiddleware):
"""Retries model calls when structured output parsing fails."""
def __init__(self, max_retries: int) -> None:
"""Initialize the structured output retry middleware.
Args:
max_retries: Maximum number of retry attempts.
"""
self.max_retries = max_retries
def wrap_model_call(
self, request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
) -> ModelResponse:
"""Intercept and control model execution via handler callback.
Args:
request: The model request containing messages and configuration.
handler: The function to call the model.
Returns:
The model response.
Raises:
StructuredOutputError: If max retries exceeded without success.
"""
for attempt in range(self.max_retries + 1):
try:
return handler(request)
except StructuredOutputError as exc:
if attempt == self.max_retries:
raise
# Include both the AI message and error in a single human message
# to maintain valid chat history alternation
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."
)
// ... (310 more lines)
Domain
Subdomains
Functions
- get_weather()
- test_structured_output_retry_exceeds_max_retries()
- test_structured_output_retry_first_attempt_invalid()
- test_structured_output_retry_preserves_messages()
- test_structured_output_retry_succeeds_first_attempt()
- test_structured_output_retry_validation_error()
- test_structured_output_retry_zero_retries()
Dependencies
- collections.abc
- langchain.agents
- langchain.agents.middleware.types
- langchain.agents.structured_output
- langchain_core.messages
- langchain_core.tools
- langgraph.checkpoint.memory
- pydantic
- pytest
- tests.unit_tests.agents.model
Source
Frequently Asked Questions
What does test_structured_output_retry.py do?
test_structured_output_retry.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, Runnables subdomain.
What functions are defined in test_structured_output_retry.py?
test_structured_output_retry.py defines 7 function(s): get_weather, test_structured_output_retry_exceeds_max_retries, test_structured_output_retry_first_attempt_invalid, test_structured_output_retry_preserves_messages, test_structured_output_retry_succeeds_first_attempt, test_structured_output_retry_validation_error, test_structured_output_retry_zero_retries.
What does test_structured_output_retry.py depend on?
test_structured_output_retry.py imports 10 module(s): collections.abc, langchain.agents, langchain.agents.middleware.types, langchain.agents.structured_output, langchain_core.messages, langchain_core.tools, langgraph.checkpoint.memory, pydantic, and 2 more.
Where is test_structured_output_retry.py in the architecture?
test_structured_output_retry.py is located at libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_structured_output_retry.py (domain: LangChainCore, subdomain: Runnables, directory: libs/langchain_v1/tests/unit_tests/agents/middleware/implementations).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free