# google/langfun architecture

> Architecture diagram of the GitHub repository google/langfun, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/google/langfun
- Repository: https://github.com/google/langfun

## Overview

Langfun is a PyGlove-powered Python library for composing language-model workflows from prompts, types, and program objects. The principal documented workflow is a user calling `lf.query` with a prompt, output schema, and model, then receiving a typed result. The graph also preserves documented evaluation, multimodal prompting, model integrations, agent/tool use, code generation, and sandboxed environments. Source evidence is uneven: some subsystem links are documented in the README or SOURCE INDEX, while several capabilities are represented by their tree paths without inferred internal call chains.

Main entry points: [`langfun/core/structured/querying.py`](https://github.com/google/langfun/blob/main/langfun/core/structured/querying.py) (Query workflow), [`langfun/core/language_model.py`](https://github.com/google/langfun/blob/main/langfun/core/language_model.py) (Model API), [`langfun/core/mcp/session.py`](https://github.com/google/langfun/blob/main/langfun/core/mcp/session.py) (MCP sessions), [`langfun/env/environment.py`](https://github.com/google/langfun/blob/main/langfun/env/environment.py) (Environment runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_structured["Structured prompting"]
  node_query["Query workflow<br/>[querying.py]"]
  node_mapping["Structured mapping<br/>[mapping.py]"]
  node_schema["Output schemas<br/>[base.py]"]
  node_prompt["Prompt templates<br/>[template.py]"]
end

subgraph group_models["Models and modalities"]
  node_model_api["Model API<br/>[language_model.py]"]
  node_model_providers["Model providers"]
  node_embedding["Embedding models<br/>[embedding_model.py]"]
  node_modalities["Media modalities"]
end

subgraph group_agents["Agents and tools"]
  node_action["Agent actions<br/>[action.py]"]
  node_mcp_client["MCP clients<br/>[client.py]"]
  node_mcp_session["MCP sessions<br/>[session.py]"]
  node_mcp_tool["MCP tools<br/>[tool.py]"]
end

subgraph group_evaluation["Evaluation"]
  node_eval["Evaluation framework"]
end

subgraph group_runtime["Code and environments"]
  node_python_code["Python code workflows"]
  node_environment["Environment runtime<br/>[environment.py]"]
  node_sandbox["Sandbox services"]
  node_env_interface["Environment interfaces<br/>[interface.py]"]
end

node_user(("Library user"))
node_external_lm{{"LLM services"}}
node_mcp_server{{"MCP servers"}}

node_user -->|"calls"| node_query
node_query -->|"uses mapping"| node_mapping
node_query -->|"uses schema"| node_schema
node_query -.->|"requests completion"| node_model_api
node_model_api -.->|"sends requests"| node_external_lm
node_model_providers -.->|"connects"| node_external_lm
node_model_api -.->|"supports media"| node_modalities
node_embedding -->|"shares model base"| node_model_api
node_user -.->|"creates actions"| node_action
node_mcp_client -->|"creates sessions"| node_mcp_session
node_mcp_session -->|"dispatches calls"| node_mcp_tool
node_mcp_session -.->|"communicates"| node_mcp_server
node_mcp_tool -->|"calls tools"| node_mcp_server
node_user -.->|"runs evaluations"| node_eval
node_user -.->|"generates code"| node_python_code
node_user -.->|"uses environments"| node_environment
node_environment -->|"implements lifecycle"| node_env_interface
node_environment -.->|"starts services"| node_sandbox
node_sandbox -->|"implements service"| node_env_interface

click node_query "https://github.com/google/langfun/blob/main/langfun/core/structured/querying.py"
click node_mapping "https://github.com/google/langfun/blob/main/langfun/core/structured/mapping.py"
click node_schema "https://github.com/google/langfun/blob/main/langfun/core/structured/schema/base.py"
click node_prompt "https://github.com/google/langfun/blob/main/langfun/core/template.py"
click node_model_api "https://github.com/google/langfun/blob/main/langfun/core/language_model.py"
click node_model_providers "https://github.com/google/langfun/tree/main/langfun/core/llms"
click node_embedding "https://github.com/google/langfun/blob/main/langfun/core/embedding_model.py"
click node_modalities "https://github.com/google/langfun/tree/main/langfun/core/modalities"
click node_action "https://github.com/google/langfun/blob/main/langfun/core/agentic/action.py"
click node_mcp_client "https://github.com/google/langfun/blob/main/langfun/core/mcp/client.py"
click node_mcp_session "https://github.com/google/langfun/blob/main/langfun/core/mcp/session.py"
click node_mcp_tool "https://github.com/google/langfun/blob/main/langfun/core/mcp/tool.py"
click node_eval "https://github.com/google/langfun/tree/main/langfun/core/eval"
click node_python_code "https://github.com/google/langfun/tree/main/langfun/core/coding/python"
click node_environment "https://github.com/google/langfun/blob/main/langfun/env/environment.py"
click node_sandbox "https://github.com/google/langfun/blob/main/langfun/env/base_sandbox_service.py"
click node_env_interface "https://github.com/google/langfun/blob/main/langfun/env/interface.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_query,node_mapping,node_schema,node_prompt,node_user toneBlue
class node_model_api,node_model_providers,node_embedding,node_modalities toneAmber
class node_action,node_mcp_client,node_mcp_session,node_mcp_tool,node_mcp_server toneMint
class node_eval toneRose
class node_python_code,node_environment,node_sandbox,node_env_interface,node_external_lm toneIndigo
```

## Components

### Structured prompting

- [Query workflow](https://github.com/google/langfun/blob/main/langfun/core/structured/querying.py): `langfun/core/structured/querying.py`
- [Structured mapping](https://github.com/google/langfun/blob/main/langfun/core/structured/mapping.py): `langfun/core/structured/mapping.py`
- [Output schemas](https://github.com/google/langfun/blob/main/langfun/core/structured/schema/base.py): `langfun/core/structured/schema/base.py`
- [Prompt templates](https://github.com/google/langfun/blob/main/langfun/core/template.py): `langfun/core/template.py`

### Models and modalities

- [Model API](https://github.com/google/langfun/blob/main/langfun/core/language_model.py): `langfun/core/language_model.py`
- [Model providers](https://github.com/google/langfun/tree/main/langfun/core/llms): `langfun/core/llms`
- [Embedding models](https://github.com/google/langfun/blob/main/langfun/core/embedding_model.py): `langfun/core/embedding_model.py`
- [Media modalities](https://github.com/google/langfun/tree/main/langfun/core/modalities): `langfun/core/modalities`

### Agents and tools

- [Agent actions](https://github.com/google/langfun/blob/main/langfun/core/agentic/action.py): `langfun/core/agentic/action.py`
- [MCP clients](https://github.com/google/langfun/blob/main/langfun/core/mcp/client.py): `langfun/core/mcp/client.py`
- [MCP sessions](https://github.com/google/langfun/blob/main/langfun/core/mcp/session.py): `langfun/core/mcp/session.py`
- [MCP tools](https://github.com/google/langfun/blob/main/langfun/core/mcp/tool.py): `langfun/core/mcp/tool.py`

### Evaluation

- [Evaluation framework](https://github.com/google/langfun/tree/main/langfun/core/eval): `langfun/core/eval`

### Code and environments

- [Python code workflows](https://github.com/google/langfun/tree/main/langfun/core/coding/python): `langfun/core/coding/python`
- [Environment runtime](https://github.com/google/langfun/blob/main/langfun/env/environment.py): `langfun/env/environment.py`
- [Sandbox services](https://github.com/google/langfun/blob/main/langfun/env/base_sandbox_service.py): `langfun/env/base_sandbox_service.py`
- [Environment interfaces](https://github.com/google/langfun/blob/main/langfun/env/interface.py): `langfun/env/interface.py`

### Other components

- **Library user**
- **LLM services**
- **MCP servers**

## Connections

- Library user → Query workflow: calls
- Query workflow → Structured mapping: uses mapping (evidence: `langfun/core/structured/querying.py`)
- Query workflow → Output schemas: uses schema (evidence: `langfun/core/structured/querying.py`)
- Query workflow → Model API: requests completion
- Model API → LLM services: sends requests
- Model providers → LLM services: connects
- Model API → Media modalities: supports media
- Embedding models → Model API: shares model base (evidence: `langfun/core/embedding_model.py`)
- Library user → Agent actions: creates actions
- MCP clients → MCP sessions: creates sessions (evidence: `langfun/core/mcp/client.py`)
- MCP sessions → MCP tools: dispatches calls (evidence: `langfun/core/mcp/session.py`)
- MCP sessions → MCP servers: communicates
- MCP tools → MCP servers: calls tools (evidence: `langfun/core/mcp/tool.py`)
- Library user → Evaluation framework: runs evaluations
- Library user → Python code workflows: generates code
- Library user → Environment runtime: uses environments
- Environment runtime → Environment interfaces: implements lifecycle (evidence: `langfun/env/environment.py`)
- Environment runtime → Sandbox services: starts services
- Sandbox services → Environment interfaces: implements service (evidence: `langfun/env/base_sandbox_service.py`)
