Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: enhance model type normalization (#13548)

sxjeru4100f2f70017
diff --git a/packages/model-runtime/src/utils/modelParse.test.ts b/packages/model-runtime/src/utils/modelParse.test.ts
index cc25d7a14a9..c7b3619f7d2 100644
--- a/packages/model-runtime/src/utils/modelParse.test.ts
+++ b/packages/model-runtime/src/utils/modelParse.test.ts
@@ -10,104 +10,109 @@ import {
} from './modelParse';
// Mock the imported LOBE_DEFAULT_MODEL_LIST
-const mockDefaultModelList: (Partial<ChatModelCard> & { id: string })[] = [
- {
- contextWindowTokens: 8192,
- displayName: 'GPT-4',
- enabled: true,
- functionCall: true,
- id: 'gpt-4',
- maxOutput: 4096,
- reasoning: false,
- vision: true,
- },
- {
- displayName: 'Claude 3 Opus',
- enabled: true,
- functionCall: true,
- id: 'claude-3-opus',
- reasoning: true,
- vision: true,
- },
- {
- displayName: 'Qwen Turbo',
- enabled: true,
- functionCall: true,
- id: 'qwen-turbo',
- reasoning: false,
- vision: false,
- },
- // Added for more detailed tests:
- {
- displayName: 'Custom Known FC True',
- enabled: true,
- functionCall: true,
- id: 'custom-model-known-fc-true', // For testing: knownModel.abilities.fc=true, no keyword match for openai fc
- reasoning: false,
- vision: false,
- },
- {
- displayName: 'GPT-4o Known FC False',
- enabled: true,
- functionCall: false,
- id: 'gpt-4o-known-fc-false', // For testing: '4o' keyword match, knownModel.abilities.fc=false
- reasoning: true,
- vision: true,
- },
- {
- displayName: 'GPT-4o Known Vision False',
- enabled: true,
- functionCall: true,
- id: 'gpt-4o-known-vision-false', // For testing: '4o' keyword match, knownModel.abilities.vision=false
- reasoning: true,
- vision: false,
- },
- {
- displayName: 'GPT-4o Audio Known Abilities True',
- enabled: true,
- functionCall: true,
- id: 'gpt-4o-audio-known-abilities-true', // For testing: '4o' keyword, 'audio' excluded, but knownModel.abilities.fc/vision=true
- reasoning: true,
- vision: true,
- },
- {
- displayName: 'GPT-4o Audio Known Abilities False',
- enabled: true,
- functionCall: false,
- id: 'gpt-4o-audio-known-abilities-false', // For testing: '4o' keyword, 'audio' excluded, and knownModel.abilities.fc/vision=false
- reasoning: false,
- vision: false,
- },
- {
- displayName: 'Known Model DisplayName',
- enabled: true,
- id: 'model-known-displayname',
- },
- {
- contextWindowTokens: 1000,
- enabled: true,
- id: 'model-known-context',
- maxOutput: 100,
- },
- {
- displayName: 'Known Disabled Model',
- enabled: false,
- id: 'model-known-disabled',
- },
- {
- displayName: 'Known Model With Settings',
- enabled: true,
- id: 'model-known-settings',
- settings: {
- extendParams: ['enableReasoning'],
- searchImpl: 'params',
- searchProvider: 'builtin',
+const { mockDefaultModelList } = vi.hoisted(() => ({
+ mockDefaultModelList: [
+ {
+ contextWindowTokens: 8192,
+ displayName: 'GPT-4',
+ enabled: true,
+ functionCall: true,
+ id: 'gpt-4',
+ maxOutput: 4096,
+ reasoning: false,
+ vision: true,
},
- },
-];
+ {
+ displayName: 'Claude 3 Opus',
+ enabled: true,
+ functionCall: true,
+ id: 'claude-3-opus',
+ reasoning: true,
+ vision: true,
+ },
+ {
+ displayName: 'Qwen Turbo',
+ enabled: true,
+ functionCall: true,
+ id: 'qwen-turbo',
+ reasoning: false,
+ vision: false,
+ },
+ // Added for more detailed tests:
+ {
+ displayName: 'Custom Known FC True',
+ enabled: true,
+ functionCall: true,
+ id: 'custom-model-known-fc-true', // For testing: knownModel.abilities.fc=true, no keyword match for openai fc
+ reasoning: false,
+ vision: false,
+ },
+ {
+ displayName: 'GPT-4o Known FC False',
+ enabled: true,
+ functionCall: false,
+ id: 'gpt-4o-known-fc-false', // For testing: '4o' keyword match, knownModel.abilities.fc=false
+ reasoning: true,
+ vision: true,
+ },
+ {
+ displayName: 'GPT-4o Known Vision False',
+ enabled: true,
+ functionCall: true,
+ id: 'gpt-4o-known-vision-false', // For testing: '4o' keyword match, knownModel.abilities.vision=false
+ reasoning: true,
+ vision: false,
+ },
+ {
+ displayName: 'GPT-4o Audio Known Abilities True',
+ enabled: true,
+ functionCall: true,
+ id: 'gpt-4o-audio-known-abilities-true', // For testing: '4o' keyword, 'audio' excluded, but knownModel.abilities.fc/vision=true
+ reasoning: true,
+ vision: true,
+ },
+ {
+ displayName: 'GPT-4o Audio Known Abilities False',
+ enabled: true,
+ functionCall: false,
+ id: 'gpt-4o-audio-known-abilities-false', // For testing: '4o' keyword, 'audio' excluded, and knownModel.abilities.fc/vision=false
+ reasoning: false,
+ vision: false,
+ },
+ {
+ displayName: 'Known Model DisplayName',
+ enabled: true,
+ id: 'model-known-displayname',
+ },
+ {
+ contextWindowTokens: 1000,
+ enabled: true,
+ id: 'model-known-context',
+ maxOutput: 100,
+ },
+ {
+ displayName: 'Known Disabled Model',
+ enabled: false,
+ id: 'model-known-disabled',
+ },
+ {
+ displayName: 'Known Model With Settings',
+ enabled: true,
+ id: 'model-known-settings',
+ settings: {
+ extendParams: ['enableReasoning'],
+ searchImpl: 'params',
+ searchProvider: 'builtin',
+ },
+ },
+ ] as (Partial<ChatModelCard> & { id: string })[],
+}));
// Mock the import
vi.mock('model-bank', () => ({
+ AiModelTypeSchema: {
+ options: ['chat', 'embedding', 'tts', 'stt', 'image', 'video', 'text2music', 'realtime'],
+ },
LOBE_DEFAULT_MODEL_LIST: mockDefaultModelList,
// 新增 provider 专用清单,供 findKnownModelByProvider 使用
google: [
@@ -248,6 +253,19 @@ describe('modelParse', () => {
expect(Array.isArray(result)).toBe(true);
});
+ it('should ignore invalid model type values and fallback to inferred types', async () => {
+ const modelList = [
+ { id: 'custom-chat-model', type: 'model' },
+ { id: 'text-embedding-3-large', type: 'model' },
+ ];
+
+ const result = await processModelList(modelList, MODEL_LIST_CONFIGS.openai);
+
+ expect(result).toHaveLength(2);
+ expect(result.find((m) => m.id === 'custom-chat-model')?.type).toBe('chat');
+ expect(result.find((m) => m.id === 'text-embedding-3-large')?.type).toBe('embedding');
+ });
+
// New search & imageOutput focused tests for single provider path
describe('search and imageOutput (processModelList)', () => {
it('openai: default search keywords should make "*-search" models support search', async () => {
@@ -518,6 +536,17 @@ describe('modelParse', () => {
expect(unknown.reasoning).toBe(false);
expect(unknown.vision).toBe(false);
});
+
+ it('should ignore invalid remote type values in mixed provider processing', async () => {
+ const modelList = [{ id: 'newapi-custom-model', type: 'model' }];
+
+ const result = await processMultiProviderModelList(modelList, 'newapi');
+
+ expect(result).toHaveLength(1);
+ expect(result[0].id).toBe('newapi-custom-model');
+ expect(result[0].type).toBe('chat');
+ });
+
it('should correctly process a model from a non-OpenAI provider not in default list, relying on keywords', async () => {
// This model ('claude-3-haiku-unlisted') is NOT in mockDefaultModelList.
// It should be detected as 'anthropic'.
diff --git a/packages/model-runtime/src/utils/modelParse.ts b/packages/model-runtime/src/utils/modelParse.ts
index ea4848f0eda..4dc59cc6a57 100644
--- a/packages/model-runtime/src/utils/modelParse.ts
+++ b/packages/model-runtime/src/utils/modelParse.ts
@@ -1,7 +1,8 @@
-import { type ChatModelCard } from '@lobechat/types';
-import { type AIBaseModelCard, type AiModelSettings, type ExtendParamsType } from 'model-bank';
+import type { ChatModelCard } from '@lobechat/types';
+import type { AIBaseModelCard, AiModelSettings, AiModelType, ExtendParamsType } from 'model-bank';
+import { AiModelTypeSchema } from 'model-bank';
-import { type ModelProviderKey } from '../types';
+import type { ModelProviderKey } from '../types';
export interface ModelProcessorConfig {
excludeKeywords?: readonly string[]; // Do not add tags to models that match
@@ -190,6 +191,20 @@ export const IMAGE_MODEL_KEYWORDS = [
// Embedding model keyword configuration
export const EMBEDDING_MODEL_KEYWORDS = ['embedding', 'embed', 'bge', 'm3e'] as const;
+const AI_MODEL_TYPE_SET = new Set<AiModelType>(AiModelTypeSchema.options);
+
+const normalizeModelType = (value: unknown): AiModelType | undefined => {
+ if (typeof value !== 'string') return undefined;
+
+ const normalized = value.toLowerCase() as AiModelType;
+
+ if (AI_MODEL_TYPE_SET.has(normalized)) {
+ return normalized;
+ }
+
+ return undefined;
+};
+
/**
* Detect whether a keyword list matches a model ID (supports multiple matching patterns)
* @param modelId Model ID (lowercase)
@@ -482,8 +497,9 @@ const processModelCard = (
} = config;
const isExcludedModel = isKeywordListMatch(model.id.toLowerCase(), excludeKeywords);
+ const normalizedModelType = normalizeModelType(model.type);
const modelType =
- model.type ||
+ normalizedModelType ||
knownModel?.type ||
(isKeywordListMatch(
model.id.toLowerCase(),