> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qwedai.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain integration

> Use QWED with LangChain via QWEDTool for verified AI chains and agents. Includes verification callbacks for math, logic, and code validation in your workflows.

## Installation

```bash theme={null}
pip install qwed langchain
```

## Quick start

```python theme={null}
from qwed_sdk.langchain import QWEDTool, QWEDVerificationCallback

# Add QWED as a tool
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI

agent = initialize_agent(
    tools=[QWEDTool()],
    llm=OpenAI(),
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)

result = agent.run("Verify: Is 2+2 equal to 5?")
# Agent uses QWED tool to verify and corrects the answer
```

## Available components

### QWEDTool

General-purpose verification tool:

```python theme={null}
from qwed_sdk.langchain import QWEDTool

tool = QWEDTool(api_key="qwed_...")
print(tool.run("2+2=4"))
# ✅ VERIFIED: The statement is correct
```

### Specialized tools

```python theme={null}
from qwed_sdk.langchain import QWEDMathTool, QWEDLogicTool, QWEDCodeTool

tools = [
    QWEDMathTool(),   # Math expressions
    QWEDLogicTool(),  # QWED-Logic DSL
    QWEDCodeTool(),   # Code security
]
```

### QWEDVerificationCallback

Auto-verify all LLM outputs:

```python theme={null}
from langchain.chains import LLMChain
from qwed_sdk.langchain import QWEDVerificationCallback

callback = QWEDVerificationCallback(
    verify_math=True,
    verify_code=True,
    log_results=True,
)

chain = LLMChain(
    llm=OpenAI(),
    prompt=prompt,
    callbacks=[callback]
)

result = chain.run("Calculate 15% of 200")
# [QWED] ✅ MATH: verified=True
```

### QWEDVerifiedChain

Wrap any chain with verification:

```python theme={null}
from qwed_sdk.langchain import QWEDVerifiedChain

base_chain = LLMChain(llm=llm, prompt=prompt)
verified_chain = QWEDVerifiedChain(base_chain, auto_correct=True)

result = verified_chain.run("What is 2+2?")
print(result.output)    # "4"
print(result.verified)  # True
```

The tools, callback, and `QWEDVerifiedChain` read the client's `is_verified`, which follows the server's admission. `result.verified` is `True` only when the server admitted the output. A `BLOCKED` or `UNVERIFIABLE` result is never verified.

> [!WARNING]
>
> Before v8.0.0 ([GHSA-83v6-6h23-pvp4](https://github.com/QWED-AI/qwed-verification/security/advisories/GHSA-83v6-6h23-pvp4)), the integrations read a `verified` attribute that `QWEDClient` results don't have, so their checks raised. When you call `QWEDClient` directly, gate on `result.is_verified`.

## LCEL integration

Use with LangChain Expression Language:

```python theme={null}
from langchain_core.runnables import RunnableLambda

def verify_output(text):
    from qwed_sdk import QWEDClient
    client = QWEDClient()
    result = client.verify(text)
    return text if result.is_verified else f"[UNVERIFIED] {text}"

chain = prompt | llm | RunnableLambda(verify_output)
```

## Best practices

1. **Use callbacks for monitoring** — Track verification rates
2. **Use tools for agent autonomy** — Let agents verify themselves
3. **Use wrappers for guarantees** — Ensure all outputs are verified


## Related topics

- [LangChain integration for verified AI agents](https://docs.qwedai.tech/open-responses/langchain.md)
- [QWED Open Responses guards for verified AI agents](https://docs.qwedai.tech/open-responses/guards.md)
- [QWED Open Responses: verified tool calls for AI agents](https://docs.qwedai.tech/open-responses/overview.md)
- [Changelog](https://docs.qwedai.tech/changelog.md)
- [QWED Protocol: deterministic verification for LLMs](https://docs.qwedai.tech/whitepaper.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.