0
# Template Development
1
2
Create and develop installable LangChain template packages with scaffolding, development server, and template discovery capabilities. This namespace provides tools for building reusable LangChain components.
3
4
## Capabilities
5
6
### Create New Template
7
8
Creates a new template package with standardized structure and configuration.
9
10
```bash { .api }
11
langchain template new [name] [options]
12
13
Arguments:
14
name The name of the folder to create (required)
15
16
Options:
17
--with-poetry Run poetry install after creation
18
--no-poetry Don't run poetry install (default)
19
```
20
21
**Usage Examples:**
22
23
```bash
24
# Create new template
25
langchain template new my-template
26
27
# Create template with poetry install
28
langchain template new my-template --with-poetry
29
30
# Create template in current directory
31
langchain template new .
32
```
33
34
**Generated Structure:**
35
36
The new command creates a complete template package with:
37
38
- `pyproject.toml` with LangServe export configuration
39
- Python module with chain implementation
40
- `README.md` with usage instructions and route code
41
- Standard package structure for LangChain templates
42
43
### Serve Template
44
45
Start a development server for testing templates with configurable options and playground interfaces.
46
47
```bash { .api }
48
langchain template serve [options]
49
50
Options:
51
--port INTEGER The port to run the server on (default: 8000)
52
--host TEXT The host to run the server on (default: 127.0.0.1)
53
--configurable/--no-configurable Whether to include a configurable route
54
--chat-playground Whether to include a chat playground route
55
--no-chat-playground Don't include chat playground (default)
56
```
57
58
**Usage Examples:**
59
60
```bash
61
# Serve template on default settings
62
langchain template serve
63
64
# Serve with chat playground
65
langchain template serve --chat-playground --port 8080
66
67
# Serve with configurable route
68
langchain template serve --configurable --host 0.0.0.0
69
70
# Serve on custom port and host
71
langchain template serve --port 9000 --host localhost
72
```
73
74
**Server Modes:**
75
76
- **Default**: Basic template serving with standard LangServe endpoints
77
- **Configurable**: Includes configuration endpoints for dynamic template parameters
78
- **Chat Playground**: Adds interactive chat interface for testing conversational templates
79
80
### List Templates
81
82
Discover available templates from the LangChain template repository.
83
84
```bash { .api }
85
langchain template list [contains]
86
87
Arguments:
88
contains Optional search term to filter templates
89
```
90
91
**Usage Examples:**
92
93
```bash
94
# List all available templates
95
langchain template list
96
97
# Search for specific templates
98
langchain template list rag
99
100
# Search for OpenAI templates
101
langchain template list openai
102
```
103
104
## Programmatic API
105
106
### Template Namespace Functions
107
108
```python { .api }
109
def new(
110
name: str,
111
with_poetry: bool = False,
112
) -> None:
113
"""Create a new template package."""
114
115
def serve(
116
*,
117
port: Optional[int] = None,
118
host: Optional[str] = None,
119
configurable: Optional[bool] = None,
120
chat_playground: bool = False,
121
) -> None:
122
"""Start a demo app for this template."""
123
124
def list(contains: Optional[str] = None) -> None:
125
"""List all or search for available templates."""
126
```
127
128
### Template Structure
129
130
Templates follow a standardized structure:
131
132
```
133
template-name/
134
├── pyproject.toml # Package configuration with LangServe export
135
├── README.md # Usage documentation and route code
136
├── template_name/ # Python package
137
│ ├── __init__.py # Module exports
138
│ └── chain.py # Chain implementation
139
└── tests/ # Test files (optional)
140
```
141
142
### LangServe Export Configuration
143
144
Templates must include LangServe export configuration in `pyproject.toml`:
145
146
```toml
147
[tool.langserve]
148
export_module = "template_name.chain"
149
export_attr = "chain"
150
151
[tool.poetry]
152
name = "template-name"
153
```
154
155
This configuration enables:
156
- Automatic discovery by the CLI
157
- Proper import resolution when adding to applications
158
- Route generation for LangServe applications
159
160
## Template Development Best Practices
161
162
### Chain Implementation
163
164
Templates should export a well-defined chain:
165
166
```python
167
from langchain_core.runnables import Runnable
168
169
# Your chain implementation
170
chain: Runnable = ...
171
```
172
173
### Module Structure
174
175
Use consistent module organization:
176
177
```python
178
# __init__.py
179
from template_name.chain import chain
180
181
__all__ = ["chain"]
182
```
183
184
### Documentation
185
186
Include comprehensive README with:
187
- Purpose and use cases
188
- Installation instructions
189
- Usage examples
190
- Route integration code
191
- Configuration options
192
193
### Testing
194
195
Templates can include tests for validation:
196
197
```python
198
def test_chain():
199
"""Test the template chain."""
200
# Test implementation
201
pass
202
```
203
204
## Development Workflow
205
206
### Local Development
207
208
1. Create template: `langchain template new my-template`
209
2. Implement chain logic in the generated module
210
3. Test locally: `langchain template serve --chat-playground`
211
4. Validate with demo server and playground interface
212
213
### Template Publishing
214
215
Templates can be published to:
216
- Git repositories for distribution
217
- LangChain template registry
218
- Private template repositories
219
220
### Integration Testing
221
222
Test template integration with applications:
223
224
```bash
225
# Create test app
226
langchain app new test-app
227
228
# Add your template
229
langchain app add /path/to/template --pip
230
231
# Test in application context
232
langchain app serve
233
```
234
235
## Error Handling
236
237
Common development issues and solutions:
238
239
- **Invalid pyproject.toml**: Missing LangServe export configuration
240
- **Import Errors**: Incorrect module structure or missing dependencies
241
- **Chain Validation**: Runtime errors in chain implementation
242
- **Server Issues**: Port conflicts or host binding problems
243
- **Template Discovery**: Network issues accessing template registry