Reversible UUID/ID aliasing for LangChain & LangGraph — cut tokens and stop the LLM mangling your IDs.
Project description
langchain-id-aliaser
Reversible UUID / ID aliasing for LangChain & LangGraph. Replace long identifiers with short, stable aliases before they reach the LLM — and restore the originals on the way back, including in the model's outgoing tool calls.
f47ac10b-58cc-4372-a567-0e02b2c3d479 → usr_a9Fk2
(~15-19 tokens) (~2 tokens)
Why
- Cheaper. A UUID tokenizes into ~15-19 tokens. In agent loops the same ids echo through tool calls, results, and state — the cost multiplies. Aliases are 1-2 tokens.
- More accurate. LLMs frequently mangle or hallucinate long UUIDs. Short, stable aliases are far harder to corrupt, so tool calls round-trip correctly.
Install
pip install langchain-id-aliaser # core only
pip install "langchain-id-aliaser[langchain]" # + adapters
How it works
One dependency-free engine (IdAliaser) detects ids, aliases them, and restores
them. Three thin adapters share that engine so behavior is identical everywhere:
| Use case | Adapter |
|---|---|
| Single call | wrap_model(model) |
| Agent | AliasMiddleware |
Raw StateGraph |
alias_before_model / restore_after_model, or make_model_node |
Core
from langchain_id_aliaser import IdAliaser
a = IdAliaser() # deterministic-hash mode, UUIDs on by default
a.register(value="cust_8f3k2", type="cust") # alias any value, with a type prefix
a.register(pattern=r"order_\d+", type="ord") # ...or a custom pattern
aliased = a.aliasify("user f47ac10b-58cc-4372-a567-0e02b2c3d479 placed order_991")
restored = a.restore(aliased) # exact inverse; unknown aliases pass through
Single call
from langchain_id_aliaser import wrap_model
model = wrap_model(chat_model) # alias in, restore out — incl. tool calls
model.invoke(messages)
Agent middleware
from langchain_id_aliaser import AliasMiddleware
agent = create_agent(model, tools, middleware=[AliasMiddleware()])
Raw graph
from langchain_id_aliaser import make_model_node
graph.add_node("model", make_model_node(chat_model))
Options
mode="hash"(default): deterministic alias from the id — same id → same alias, no stored state, stable across turns.mode="ordinal":id1, id2 …(shorter, but the mapping must be reused to stay consistent).strict=True: raiseUnknownAliasErrorwhen the model emits an alias-shaped token that isn't in the mapping. Default is forgiving (pass through).
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_id_aliaser-0.1.0.tar.gz.
File metadata
- Download URL: langchain_id_aliaser-0.1.0.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d23a4cfd351a7f392b5fb6d20bd3d4bed67241711838ba99dc7119512b7d4c06
|
|
| MD5 |
12b01b9107b610e74eecc8885b42c251
|
|
| BLAKE2b-256 |
6c4c1208d3a2523f40c5552583deb6cbff89e303fdb05c7138c886c27e57a63c
|
Provenance
The following attestation bundles were made for langchain_id_aliaser-0.1.0.tar.gz:
Publisher:
publish.yml on augustovillar/langchain-id-aliaser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_id_aliaser-0.1.0.tar.gz -
Subject digest:
d23a4cfd351a7f392b5fb6d20bd3d4bed67241711838ba99dc7119512b7d4c06 - Sigstore transparency entry: 1644794600
- Sigstore integration time:
-
Permalink:
augustovillar/langchain-id-aliaser@dbbbd1a0a24169ff4c3105976db109df8e6ca447 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/augustovillar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dbbbd1a0a24169ff4c3105976db109df8e6ca447 -
Trigger Event:
release
-
Statement type:
File details
Details for the file langchain_id_aliaser-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_id_aliaser-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d594f530e3d2209257e7ad4608b613b8ffa3b6250ea1aec772e9f660487747e2
|
|
| MD5 |
21cee23e9447cf3f4142551fb129cf83
|
|
| BLAKE2b-256 |
14ce6e65fb1f1053edeac7fba615b71f927d812af7f83a86fe1654e881063469
|
Provenance
The following attestation bundles were made for langchain_id_aliaser-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on augustovillar/langchain-id-aliaser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_id_aliaser-0.1.0-py3-none-any.whl -
Subject digest:
d594f530e3d2209257e7ad4608b613b8ffa3b6250ea1aec772e9f660487747e2 - Sigstore transparency entry: 1644795113
- Sigstore integration time:
-
Permalink:
augustovillar/langchain-id-aliaser@dbbbd1a0a24169ff4c3105976db109df8e6ca447 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/augustovillar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dbbbd1a0a24169ff4c3105976db109df8e6ca447 -
Trigger Event:
release
-
Statement type: