Skip to main content

OpenTerms adapter for LangChain — sign and emit ORS v0.1 receipts for every tool call.

Project description

langchain-openterms

LangChain adapter for OpenTerms — emits a signed ORS v0.1 receipt for every tool invocation in a LangChain chain or agent.

Depends on openterms-py>=0.1.0 for canonicalization, signing, and the HTTP client. One signing path is shared with crewai-openterms.

Install

pip install langchain-openterms

This pulls in langchain-core>=0.3,<1.0. The adapter uses only the public BaseCallbackHandler hooks, which have been stable through the 0.3 line. If you need to run against an older LangChain version, pin langchain-core and report compatibility issues on the repo.

Quickstart

from langchain_core.tools import tool
from openterms import IngestClient, generate_keypair
from openterms_langchain import OpenTermsCallbackHandler

sk, _ = generate_keypair()
client = IngestClient(
    base_url="http://localhost:3000",
    workspace_id="00000000-0000-4000-8000-0000000000aa",
    key_id="my-key",
    private_key=sk.private_bytes_raw(),
    agent_id="my-agent",
)
handler = OpenTermsCallbackHandler(
    client=client,
    agent_id="my-agent",
    terms_url="https://example.com/terms",
    terms_hash="a" * 64,
    emit_post_action=True,
)

@tool
def add(a: int, b: int) -> int:
    """Add two integers."""
    return a + b

add.invoke({"a": 2, "b": 3}, config={"callbacks": [handler]})

Two receipts will be emitted per call: one on on_tool_start with the inputs, and one on on_tool_end with action_context.post_state_hash = SHA-256 of the output.

See examples/quickstart.py for a runnable copy.

Configuration

  • terms_url / terms_hash: the default terms binding for every receipt. Override per-tool with handler.set_tool_terms(tool_name, terms_url=..., terms_hash=...).
  • emit_post_action: when True, emit a second receipt on on_tool_end. Default False.
  • strict: when True, re-raise openterms.IngestError from the callback so a failed emission propagates up the chain. Default False — failures are logged and swallowed so a misbehaving ingest cannot break an agent loop.

What this adapter does

  • Hooks LangChain's on_tool_start / on_tool_end / on_tool_error lifecycle.
  • Builds an ORS receipt with action_type="tool_call" and action_context = {tool_id, args}.
  • Signs and POSTs through openterms.IngestClient.
  • Optionally emits a second receipt on tool completion containing a post_state_hash (SHA-256 of the stringified output).

What this adapter does NOT do

  • It does not run an ingest service. You must point IngestClient at a running OpenTerms API. See apps/api and IMPLEMENTATION_STATUS.md.
  • It does not handle auth. The bearer-token placeholder on IngestClient is not yet enforced by the service.
  • It does not host a JWKS. Verification requires a JWKS you supply.
  • It does not buffer or retry. A failed emission is logged once (or re-raised if strict=True) and the call proceeds.
  • It does not patch LangChain. The handler is purely additive — you attach it via the callbacks config argument and remove it the same way.

Repository

Source and issue tracker: jstibal/openterms-trace.

License

Apache-2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_openterms-1.0.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_openterms-1.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_openterms-1.0.0.tar.gz.

File metadata

  • Download URL: langchain_openterms-1.0.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for langchain_openterms-1.0.0.tar.gz
Algorithm Hash digest
SHA256 99a0ba7a38edec641fd48ce082e82505a70fa5272f9536a908a2e1cf7a574023
MD5 2d13d0fd45a536a70949ccd8e4456009
BLAKE2b-256 d4fb2037a691c73edafcc6c7cd5dd0a923497ca33bd848eb8f5b8cd90b584d5b

See more details on using hashes here.

File details

Details for the file langchain_openterms-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_openterms-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7782014fcdf0fb4a3ae312a4e1dc1643299ed450991c0b80a015625d2c7ca8ec
MD5 a9babdabd9fc422d1789abde33d70fed
BLAKE2b-256 659cfacd67af666a9ce7009b263639f45a37339c728302ab0ff5650435d2ad09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page