Home / Function/ test_convert_error_to_response() — langchain Function Reference

test_convert_error_to_response() — langchain Function Reference

Architecture documentation for the test_convert_error_to_response() function in test_wrap_model_call.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  1ddb5742_9c97_c187_6417_15b8741aa77f["test_convert_error_to_response()"]
  af226bc7_9003_a498_4aba_5a5b0a9f8c0c["TestErrorHandling"]
  1ddb5742_9c97_c187_6417_15b8741aa77f -->|defined in| af226bc7_9003_a498_4aba_5a5b0a9f8c0c
  style 1ddb5742_9c97_c187_6417_15b8741aa77f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call.py lines 377–410

    def test_convert_error_to_response(self) -> None:
        """Test middleware that converts errors to successful responses."""

        class AlwaysFailModel(GenericFakeChatModel):
            @override
            def _generate(
                self,
                messages: list[BaseMessage],
                stop: list[str] | None = None,
                run_manager: CallbackManagerForLLMRun | None = None,
                **kwargs: Any,
            ) -> ChatResult:
                msg = "Model error"
                raise ValueError(msg)

        class ErrorToSuccessMiddleware(AgentMiddleware):
            def wrap_model_call(
                self,
                request: ModelRequest,
                handler: Callable[[ModelRequest], ModelResponse],
            ) -> ModelCallResult:
                try:
                    return handler(request)
                except Exception as e:
                    return AIMessage(content=f"Error occurred: {e}. Using fallback response.")

        model = AlwaysFailModel(messages=iter([]))
        agent = create_agent(model=model, middleware=[ErrorToSuccessMiddleware()])

        # Should not raise, middleware converts error to response
        result = agent.invoke({"messages": [HumanMessage("Test")]})

        assert "Error occurred" in result["messages"][1].content
        assert "fallback response" in result["messages"][1].content

Domain

Subdomains

Frequently Asked Questions

What does test_convert_error_to_response() do?
test_convert_error_to_response() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call.py.
Where is test_convert_error_to_response() defined?
test_convert_error_to_response() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call.py at line 377.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free