Skip to main content
DeerFlow’s model factory (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()

Location: backend/src/models/factory.py:11-64

Model Configuration

config.yaml Structure

Field Reference

Core Fields

  • name (required): Unique identifier for model selection
  • display_name: Human-readable name for UI
  • description: Short description of model capabilities
  • use (required): Python import path for model class (e.g., langchain_openai:ChatOpenAI)

Capability Flags

  • supports_thinking: Model supports extended thinking/reasoning modes
  • supports_reasoning_effort: Model supports OpenAI’s reasoning_effort parameter
  • supports_vision: Model can process images (enables ViewImageMiddleware)

Provider Parameters

All additional fields passed to model constructor:

when_thinking_enabled

Overrides applied when thinking_enabled=True:

Reflection-Based Instantiation

The factory uses the reflection system to dynamically load model classes:
Benefits:
  • 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

Behavior: Model generates hidden thinking before response.

OpenAI o1 Reasoning Effort

Behavior: Controls o1 model’s reasoning intensity.

Anthropic Extended Thinking

Behavior: Claude uses <thinking> tags in response.

Vision Support

Models with supports_vision: true enable vision-specific features:

Configuration

Impact on Agent

Middleware Chain (backend/src/agents/lead_agent/agent.py:236-241):
Tool Injection (backend/src/tools/__init__.py):

Runtime Parameter Overrides

Via Keyword Arguments

Merge Behavior

Environment Variable Resolution

Config values starting with $ are resolved from environment:
Resolution (handled by Pydantic config loader):
Missing Environment Variables: Raise error at startup.

Error Handling

Missing Model

Missing Provider

Actionable Hints (from reflection system):

Thinking Not Supported

LangSmith Tracing Integration

Factory automatically attaches LangSmith tracer if enabled:
Configuration (config.yaml):

Custom Model Providers

Adding Custom Providers

  1. Install provider package:
  2. Add to config.yaml:
  3. Set environment variables:
  4. Use model:

Patched Providers

For providers needing workarounds, create patched classes: Example: backend/src/models/patched_deepseek.py
Usage in config:

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