Docs/LangChain
Guides

LangChain

Wrap ReceiptConverter as a LangChain Tool and plug it into any agent, chain, or pipeline. Build agents that parse receipts, categorize expenses, and write reports autonomously.

Install

pip install langchain langchain-openai requests

Define the tool

Create a LangChain Tool that calls the ReceiptConverter API:

import requests
from langchain.tools import Tool

RECEIPT_API_KEY = "sk_live_your_key_here"
RECEIPT_API_URL = "https://receiptconverter.com/api/v1/convert"

def parse_receipt(source: str) -> str:
    """
    Parse a receipt or invoice from a URL or local file path.
    Returns structured JSON as a string.
    source: public URL or absolute file path
    """
    headers = {"Authorization": f"Bearer {RECEIPT_API_KEY}"}

    if source.startswith("http"):
        resp = requests.post(
            RECEIPT_API_URL,
            headers={**headers, "Content-Type": "application/json"},
            json={"url": source},
            timeout=60,
        )
    else:
        with open(source, "rb") as f:
            resp = requests.post(
                RECEIPT_API_URL,
                headers=headers,
                files={"file": f},
                timeout=60,
            )

    if not resp.ok:
        error = resp.json().get("error", resp.reason)
        return f"Error parsing receipt: {error}"

    import json
    return json.dumps(resp.json()["data"], indent=2)


receipt_tool = Tool(
    name="convert_receipt",
    func=parse_receipt,
    description=(
        "Parse a receipt or invoice image/PDF into structured JSON. "
        "Input: a public URL (starting with http) or an absolute file path. "
        "Returns: vendor, date, total, subtotal, tip, currency, payment method, "
        "expense category, line items with prices, and tax breakdown."
    ),
)

ReAct agent

Build a ReAct agent that can parse receipts and reason about the results:

from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub

llm = ChatOpenAI(model="gpt-4o", temperature=0)
tools = [receipt_tool]

# Pull the ReAct prompt from LangChain Hub
prompt = hub.pull("hwchase17/react")

agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Run it
result = executor.invoke({
    "input": "Parse this receipt and tell me the total and expense category: "
             "https://example.com/starbucks.jpg"
})
print(result["output"])

Simple chain (no agent loop)

If you don't need an agent loop, use a simple chain with RunnableLambda:

from langchain_core.runnables import RunnableLambda
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
import json

llm = ChatOpenAI(model="gpt-4o")

# Step 1: parse the receipt
parse_step = RunnableLambda(parse_receipt)

# Step 2: summarize and categorize
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expense management assistant. Given raw receipt JSON, "
               "produce a one-line expense entry: 'YYYY-MM-DD | Vendor | $Amount | Category'."),
    ("human", "{receipt_data}"),
])

# Chain them together
expense_chain = (
    {"receipt_data": parse_step}
    | prompt
    | llm
)

result = expense_chain.invoke("https://example.com/lunch.jpg")
print(result.content)
# → "2024-03-15 | Starbucks | $12.50 | Food & Drink"

Batch processing with LangChain

from langchain_core.runnables import RunnableLambda
from concurrent.futures import ThreadPoolExecutor
import json

def process_receipt_batch(sources: list[str], max_workers: int = 5) -> list[dict]:
    """Parse multiple receipts in parallel using LangChain runnables."""
    
    parse_and_summarize = (
        RunnableLambda(parse_receipt)
        | RunnableLambda(lambda data: {
            "raw": json.loads(data) if isinstance(data, str) and not data.startswith("Error") else None,
            "error": data if isinstance(data, str) and data.startswith("Error") else None,
        })
    )

    results = parse_and_summarize.batch(
        sources,
        config={"max_concurrency": max_workers},
    )
    return results

receipts = [
    "https://example.com/receipt1.jpg",
    "https://example.com/receipt2.jpg",
    "/Users/me/Downloads/hotel.pdf",
]

results = process_receipt_batch(receipts)
for r in results:
    if r["raw"]:
        vendor = r["raw"]["vendor"]
        total = r["raw"]["total"]
        print(vendor + " — $" + str(total))
    else:
        print("Failed: " + r["error"])

Auto-import via OpenAPI

LangChain can also auto-generate tools from the ReceiptConverter OpenAPI spec using the OpenAPI toolkit:

from langchain_community.agent_toolkits.openapi import planner
from langchain_community.utilities import RequestsWrapper
import requests

# Load the OpenAPI spec
spec = requests.get("https://receiptconverter.com/api/v1/openapi.json").json()

# Create authenticated requests wrapper
headers = {"Authorization": "Bearer sk_live_your_key_here"}
requests_wrapper = RequestsWrapper(headers=headers)

# Auto-generate tools from the spec
llm = ChatOpenAI(model="gpt-4o")
agent = planner.create_openapi_agent(spec, requests_wrapper, llm)

result = agent.run("Parse this receipt: https://example.com/receipt.jpg")
print(result)