Architecture & Purity
The 3-tier monorepo architecture, effect polymorphism, and zero-unsafePerformIO guarantee.
Monorepo Architecture
langchain-hs is split into three independent layers to guarantee modularity and zero unnecessary runtime dependencies:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β langchain-hs β
β Providers (Ollama, OpenAI, Gemini) Β· MCP Client Β· RAG β
β Chains Β· Vector Stores Β· Agents Β· OpenTelemetry Observabilityβ
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β depends on
ββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ
β langchain-hs-graph β
β StateGraph s m Β· StateReducer s Β· Checkpointers Β· HITL β
β TimeTravel Β· Parallel Execution Β· Graphviz DOT Export β
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β depends on
ββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ
β langchain-hs-core β
β Zero-Dependency Pure GADT ASTs (RunnableTree) β
β ChatModel Β· ContentBlock Β· Tool m Β· StreamEvent Lifecycle β
β Zero HTTP / Network / Database Dependencies β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1. Zero unsafePerformIO Guarantee
In many imperative AI frameworks, state mutation, network I/O, and tool executions are side-effecting operations scattered throughout code. In langchain-hs:
- Construction is Pure: Creating a
RunnableTree,StateGraph, orChainproduces a pure data structure AST without executing any network or disk operations. - Execution is Explicit: All effects are tracked in the monad
m(e.g.ΒIO,LangchainT m, or custom monad stacks). - Zero Hidden Global State: State is explicitly passed or held in STM
TVarcells with ACID properties.
2. Algebraic Laws & QuickCheck Properties
Components in langchain-hs are verified against standard algebraic laws:
1. StateReducer Monoid Associativity
State reducers must satisfy associativity so that concurrent parallel branches can be merged in any order without race conditions:
βa,βb,βc.Β (aβb)βcββ‘βaβ(bβc)
2. Runnable Identity & Composition Laws
Pure runnable pipelines satisfy Category / Arrow laws:
interpret (id|ββ«f)ββ‘βinterpret f interpret (f|ββ«id)ββ‘βinterpret f
3. Effect Polymorphism
All models and tools are polymorphic over their execution monad m:
-- Effect-polymorphic ChatModel
class (MonadIO m, MonadError LangchainError m) => ChatModel model m where
invoke :: model -> [Message] -> m Message
streamModel :: model -> [Message] -> (StreamEvent -> m ()) -> m ()This allows you to run pipelines inside unit test harnesses with Identity or State monads, or in production web servers inside custom ReaderT AppEnv (ExceptT AppError IO).