Skip to main content

langchaint

langchaint provides typed generation and embeddings with raw provider responses and per-attempt billing. Alpha: the API is unstable and may change without notice.

Purpose

langchaint provides provider-neutral async BoundLLM and EmbeddingModel interfaces. langchaint provides no agent class or agent loop. langchaint uses pyrefly's strict all preset. Applications should use a strict type checker because langchaint leaves argument-type checks to static checking.

Install and authenticate

langchaint requires Python 3.13 or newer. Applications install and pin each provider SDK. langchaint declares no dependency extras. Top-level import langchaint requires no provider SDK or numpy. Backend imports report missing dependencies through ModuleNotFoundError.

Provider Class Creates Depends Default credentials
Anthropic Anthropic LLM anthropic ANTHROPIC_API_KEY
Amazon Bedrock AnthropicBedrock LLM anthropic[bedrock] AWS credential provider chain
Amazon Bedrock CohereBedrock EmbeddingModel boto3 and numpy AWS credential provider chain
DeepSeek DeepSeek LLM openai DEEPSEEK_API_KEY
Gemini Gemini LLM google-genai GOOGLE_API_KEY or GEMINI_API_KEY
OpenAI OpenAI LLM, EmbeddingModel openai (numpy and tiktoken for embeddings) OPENAI_API_KEY
Amazon Bedrock OpenAIBedrock LLM openai[bedrock] AWS credential provider chain

Every listed class accepts client=. Amazon Bedrock classes use the AWS credential provider chain. It includes environment credentials, profiles, SSO, containers, and instance roles. Pass aws_region= to select a Bedrock region explicitly.

Generation quickstart

import asyncio

from langchaint.openai import OpenAI


async def main() -> None:
    openai = OpenAI()
    bound_llm = openai.model("gpt-5.6-terra").bind(system_prompt="Answer clearly and concisely.")
    response = await bound_llm.generate_one("Why is the sky blue?")
    print(response.output, response.usage.cost_in_usd)


asyncio.run(main())

response.output is assistant text. regional_processing=False uses the standard 1.0 token-price multiplier. Use regional_processing=True for an endpoint with regional processing. Pass a Pydantic model to LLM.bind(response_format=...) for validated structured output.

Embedding quickstart

import asyncio

from langchaint.openai import OpenAI


async def main() -> None:
    openai = OpenAI()
    embedding_model = openai.embedding_model(
        "text-embedding-3-small",
        dimension=1024,
    )
    documents = await embedding_model.embed(
        ["The Moon orbits Earth.", "Mars has two small moons."],
        task="retrieval_document",
    )
    query = await embedding_model.embed(
        ["Which object circles Earth?"],
        task="retrieval_query",
    )
    print(documents.shape, query.shape)


asyncio.run(main())

EmbeddingModel.embed() requires task for every adapter. The OpenAI adapter sends no task request field. EmbeddingModel.embed() returns normalized Float2D values with numpy.float32 elements. Each input produces one row.

Use CohereBedrock for Cohere embeddings through Amazon Bedrock.

Share one rate-limit quota

Create one OpenAI per rate-limit quota.

openai = OpenAI(
    max_concurrent_requests=8,
    max_request_starts_per_second=50.0,
)
terra = openai.model("gpt-5.6-terra")
sol = openai.model("gpt-5.6-sol")

terra and sol share openai.client and one SharedBackoff. max_concurrent_requests and max_request_starts_per_second apply across both LLM values. An EmbeddingModel from openai.embedding_model() uses the same client and SharedBackoff.

Pass an SDK client to close it directly.

from openai import AsyncOpenAI

client = AsyncOpenAI()
openai = OpenAI(client=client)
terra = openai.model("gpt-5.6-terra")

# Use terra.

await client.close()

Binding and results

Call LLM.bind() before generate_one, generate_many, or stream_one. Use BoundLLM.rebind() to replace selected binding fields. Pass tools=[tool] to LLM.bind() and dispatch through BoundLLM.tool_manager. LLM.bind(max_attempts=...) limits requests for one GenerationInput, including the first. embedding_model(max_attempts=...) limits requests for one batch of inputs sent together, including the first. LLM.bind() uses Adapter.automatic_cache_breakpoints_default unless automatic_cache_breakpoints overrides it. cache_breakpoint=True requests an explicit breakpoint at that ContentPart. GenerateResult and GenerationError include paid Usage across attempts.

More examples

examples/README.md indexes migration guidance and examples. The examples cover structured output, batches, streaming, tools, tracing, pricing, failures, prompt caching, reasoning, embeddings, and application structure.

Development

Run scripts/CI.sh before committing. The tests are offline and require no API keys.

License

langchaint uses the MIT License.

Release files for langchaint 0.17.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchaint 0.17.1
File Size Uploaded
langchaint-0.17.1.tar.gz 166.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchaint 0.17.1
File Interpreter ABI Platform
langchaint-0.17.1-py3-none-any.whl Python 3 none any Details

Total release size: 350.6 kB

Release files / langchaint-0.17.1.tar.gz

Download URL langchaint-0.17.1.tar.gz
Size 166.6 kB
Tags Source
SHA-256 checksum
How to use checksums
88690df6a95236535c8cf7517a29ca5e5b9b1d257c3ef1689f05c6b285669067
BLAKE2b-256 checksum
How to use checksums
037bd2092dd4b345fada1b2e852bb4581fc97052c21b24f56aae4eda7995da22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / langchaint-0.17.1-py3-none-any.whl

Download URL langchaint-0.17.1-py3-none-any.whl
Size 183.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdb7f3b7ac783fe765dab609e1b9779d6a1f7d80238a900a6d61f8749a8e0d2e
BLAKE2b-256 checksum
How to use checksums
f202e8c2700c232da5f6f97ea5223bdadd80b52ac54d1dc2cf17023f78fe48c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.23.3

2 release files

0.23.2

2 release files

0.23.1

2 release files

0.23.0

2 release files

0.22.0

2 release files

0.21.2

2 release files

0.19.4

2 release files

0.19.3

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.18.1

2 release files

0.18.0

2 release files

This release

0.17.1 This release

2 release files

0.17.0

2 release files

0.16.0

2 release files

0.15.3

2 release files

0.15.2

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.11.2

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page