Skip to main content

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: raise UnknownAliasError when the model emits an alias-shaped token that isn't in the mapping. Default is forgiving (pass through).

License

MIT

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_id_aliaser-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

langchain_id_aliaser-0.1.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

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

Hashes for langchain_id_aliaser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d23a4cfd351a7f392b5fb6d20bd3d4bed67241711838ba99dc7119512b7d4c06
MD5 12b01b9107b610e74eecc8885b42c251
BLAKE2b-256 6c4c1208d3a2523f40c5552583deb6cbff89e303fdb05c7138c886c27e57a63c

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_id_aliaser-0.1.0.tar.gz:

Publisher: publish.yml on augustovillar/langchain-id-aliaser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_id_aliaser-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_id_aliaser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d594f530e3d2209257e7ad4608b613b8ffa3b6250ea1aec772e9f660487747e2
MD5 21cee23e9447cf3f4142551fb129cf83
BLAKE2b-256 14ce6e65fb1f1053edeac7fba615b71f927d812af7f83a86fe1654e881063469

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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