backend/src/models/factory.py) provides reflection-based model instantiation from configuration files, enabling dynamic provider support without hardcoding imports. The factory handles thinking modes, vision capabilities, and runtime parameter overrides.
Core Function: create_chat_model()
backend/src/models/factory.py:11-64
Model Configuration
config.yaml Structure
Field Reference
Core Fields
name(required): Unique identifier for model selectiondisplay_name: Human-readable name for UIdescription: Short description of model capabilitiesuse(required): Python import path for model class (e.g.,langchain_openai:ChatOpenAI)
Capability Flags
supports_thinking: Model supports extended thinking/reasoning modessupports_reasoning_effort: Model supports OpenAI’sreasoning_effortparametersupports_vision: Model can process images (enablesViewImageMiddleware)
Provider Parameters
All additional fields passed to model constructor:when_thinking_enabled
Overrides applied whenthinking_enabled=True:
Reflection-Based Instantiation
The factory uses the reflection system to dynamically load model classes:- No hardcoded imports (
from langchain_openai import ChatOpenAI) - Add new providers without code changes (just update
config.yaml) - Clear error messages for missing dependencies
Thinking Mode Support
Enabling Thinking
Implementation
Thinking Configurations
OpenAI Extended Thinking
OpenAI o1 Reasoning Effort
Anthropic Extended Thinking
<thinking> tags in response.
Vision Support
Models withsupports_vision: true enable vision-specific features:
Configuration
Impact on Agent
Middleware Chain (backend/src/agents/lead_agent/agent.py:236-241):
backend/src/tools/__init__.py):
Runtime Parameter Overrides
Via Keyword Arguments
Merge Behavior
Environment Variable Resolution
Config values starting with$ are resolved from environment:
Error Handling
Missing Model
Missing Provider
Thinking Not Supported
LangSmith Tracing Integration
Factory automatically attaches LangSmith tracer if enabled:config.yaml):
Custom Model Providers
Adding Custom Providers
-
Install provider package:
-
Add to config.yaml:
-
Set environment variables:
-
Use model:
Patched Providers
For providers needing workarounds, create patched classes: Example:backend/src/models/patched_deepseek.py
Usage in Agent System
Lead Agent Creation
Summarization Middleware
Title Generation
Testing Models
Best Practices
1. Model Selection
2. Configuration Management
3. Environment Variables
4. Error Handling
See Also
- Reflection System - Dynamic class loading
- Middleware Chain - How models integrate with middlewares
- Configuration - Model configuration reference