Home / Class/ TestCustomDetector Class — langchain Architecture

TestCustomDetector Class — langchain Architecture

Architecture documentation for the TestCustomDetector class in test_pii.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e5e43364_b415_657a_0443_07c4b33daca1["TestCustomDetector"]
  6dcffc03_fae9_01f8_ed9b_d30d8a73dd13["test_pii.py"]
  e5e43364_b415_657a_0443_07c4b33daca1 -->|defined in| 6dcffc03_fae9_01f8_ed9b_d30d8a73dd13
  7fa4ec6c_97ae_74f3_0e39_4be3058c18f4["test_custom_regex_detector()"]
  e5e43364_b415_657a_0443_07c4b33daca1 -->|method| 7fa4ec6c_97ae_74f3_0e39_4be3058c18f4
  1360b6fe_5bcd_382a_b076_d28c73bb0fba["test_custom_callable_detector()"]
  e5e43364_b415_657a_0443_07c4b33daca1 -->|method| 1360b6fe_5bcd_382a_b076_d28c73bb0fba
  5a91eeff_d879_1898_37e0_b6797e65c6be["test_unknown_builtin_type_raises_error()"]
  e5e43364_b415_657a_0443_07c4b33daca1 -->|method| 5a91eeff_d879_1898_37e0_b6797e65c6be
  0b2d1997_5d90_8eb0_7b29_2c1ca3a6a826["test_custom_type_without_detector_raises_error()"]
  e5e43364_b415_657a_0443_07c4b33daca1 -->|method| 0b2d1997_5d90_8eb0_7b29_2c1ca3a6a826

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_pii.py lines 515–566

class TestCustomDetector:
    """Test custom detector functionality."""

    def test_custom_regex_detector(self) -> None:
        # Custom regex for API keys
        middleware = PIIMiddleware(
            "api_key",
            detector=r"sk-[a-zA-Z0-9]{32}",
            strategy="redact",
        )

        state = AgentState[Any](messages=[HumanMessage("Key: sk-abcdefghijklmnopqrstuvwxyz123456")])
        result = middleware.before_model(state, Runtime())

        assert result is not None
        assert "[REDACTED_API_KEY]" in result["messages"][0].content

    def test_custom_callable_detector(self) -> None:
        # Custom detector function
        def detect_custom(content: str) -> list[PIIMatch]:
            matches = []
            if "CONFIDENTIAL" in content:
                idx = content.index("CONFIDENTIAL")
                matches.append(
                    PIIMatch(
                        type="confidential",
                        value="CONFIDENTIAL",
                        start=idx,
                        end=idx + 12,
                    )
                )
            return matches

        middleware = PIIMiddleware(
            "confidential",
            detector=detect_custom,
            strategy="redact",
        )

        state = AgentState[Any](messages=[HumanMessage("This is CONFIDENTIAL information")])
        result = middleware.before_model(state, Runtime())

        assert result is not None
        assert "[REDACTED_CONFIDENTIAL]" in result["messages"][0].content

    def test_unknown_builtin_type_raises_error(self) -> None:
        with pytest.raises(ValueError, match="Unknown PII type"):
            PIIMiddleware("unknown_type", strategy="redact")

    def test_custom_type_without_detector_raises_error(self) -> None:
        with pytest.raises(ValueError, match="Unknown PII type"):
            PIIMiddleware("custom_type", strategy="redact")

Frequently Asked Questions

What is the TestCustomDetector class?
TestCustomDetector is a class in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_pii.py.
Where is TestCustomDetector defined?
TestCustomDetector is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_pii.py at line 515.

Analyze Your Own Codebase

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

Try Supermodel Free