[PR #29885] fix(agent): fix model type mismatch error in agent node with multiple model selectors #32601

Open
opened 2026-02-21 20:51:43 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/29885

State: open
Merged: No


Summary

  • Fix "Model type instance is not LargeLanguageModel" error when using custom agent strategy with multiple models (LLM + Embedding model)
  • The error occurs on the second query when memory is enabled

Problem

When using a custom agent strategy with multiple model selectors (e.g., one LLM model and one Embedding model), the workflow fails on the second query with error:

RuntimeError: Model type instance is not LargeLanguageModel

Root Cause

  1. _fetch_model() method: Hardcoded ModelType.LLM when calling get_provider_model_bundle() and get_current_credentials(), but used the actual model type from config when calling get_model_instance(). This caused a type mismatch for non-LLM models.

  2. _generate_agent_parameters() method: Attempted to create memory for ALL MODEL_SELECTOR parameters, but memory requires LLM model to calculate tokens via get_llm_num_tokens(). When processing embedding model parameters, it failed because embedding models don't support this method.

Solution

  1. Use the actual model type from config consistently in _fetch_model() method
  2. Only create memory when the model type is LLM in _generate_agent_parameters()

Test plan

  • Create an agent strategy with both LLM and Embedding model selectors
  • Enable memory feature
  • Send multiple consecutive queries
  • Verify no error occurs on subsequent queries

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/langgenius/dify/pull/29885 **State:** open **Merged:** No --- ## Summary - Fix "Model type instance is not LargeLanguageModel" error when using custom agent strategy with multiple models (LLM + Embedding model) - The error occurs on the second query when memory is enabled ## Problem When using a custom agent strategy with multiple model selectors (e.g., one LLM model and one Embedding model), the workflow fails on the second query with error: ``` RuntimeError: Model type instance is not LargeLanguageModel ``` ## Root Cause 1. **`_fetch_model()` method**: Hardcoded `ModelType.LLM` when calling `get_provider_model_bundle()` and `get_current_credentials()`, but used the actual model type from config when calling `get_model_instance()`. This caused a type mismatch for non-LLM models. 2. **`_generate_agent_parameters()` method**: Attempted to create memory for ALL `MODEL_SELECTOR` parameters, but memory requires LLM model to calculate tokens via `get_llm_num_tokens()`. When processing embedding model parameters, it failed because embedding models don't support this method. ## Solution 1. Use the actual model type from config consistently in `_fetch_model()` method 2. Only create memory when the model type is LLM in `_generate_agent_parameters()` ## Test plan - [x] Create an agent strategy with both LLM and Embedding model selectors - [x] Enable memory feature - [x] Send multiple consecutive queries - [x] Verify no error occurs on subsequent queries 🤖 Generated with [Claude Code](https://claude.com/claude-code)
yindo added the pull-request label 2026-02-21 20:51:43 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32601