kmail.at
← learning

langchain · difficulty ◆◆◆

Disallow Any Generics

Kill the catch-all Any before it reaches production.

One flag that makes your type contracts legally enforceable.

2026-06-22 · 7 min read

$ DISALLOW_ANY_GENERICS=true

What it does

disallow_any_generics is a type-checking flag in langchain-core that enforces strict generic-type discipline. When enabled, a class that uses Any as a generic parameter, such as Runnable[dict, Any], raises a TypeError at runtime or fails Pydantic schema validation. It forces precise input/output types instead of the catch-all Any.

Why it matters

Weakly typed LLM pipelines are a common source of bugs: a ChatModel emits a BaseMessage, your chain expects a str, and you hunt an AttributeError for hours. This flag closes that gap at the source and acts as a CI guardrail for teams shipping SDKs built on LangChain.

Example

$ def my_func(x: str) -> str:
    return x.upper()
runnable = RunnableLambda(my_func)
schema = config_schema(runnable)
print("Schema fields:", list(schema.get("fields", {}).keys()))
print("Type safety: PASS")
Schema fields: ['func', 'name', 'tags', 'metadata', 'recurse', 'step']
Type safety: PASS

Common flags

Runnable[Input, Output]
TypedRunnable generic specifying exact input/output types
config_schema(runnable)
Extracts the Pydantic config schema for a runnable
disallow_any_generics
Runtime check rejecting Any as a generic type parameter

History

Typed runnables, typed pipelines

LangChain has steadily pushed toward precise typing. The disallow_any_generics flag makes that discipline enforceable in CI, catching regressions in code review instead of production.

Fun facts

Pros & cons

pros

  • + Catches type bugs before production
  • + Enforceable in CI
  • + Clear, actionable failures

cons

  • − Requires refactoring existing Any usages
  • − Strictness may slow onboarding

Takeaways

  1. 1Any as a generic type parameter is banned when the flag is on.
  2. 2Use config_schema() to inspect runnable contracts.
  3. 3Run it in CI to keep type rigour enforced.

Related commands

← all learning