fix(pricing): guard malformed models.dev model entries (#1344)

* fix(pricing): guard malformed models.dev model entries

* style: apply prettier formatting
This commit is contained in:
Kai (Tam Nhu) Tran
2026-05-23 16:55:05 -04:00
committed by GitHub
parent af7aa0c2dd
commit 66966f3527
2 changed files with 22 additions and 0 deletions
@@ -91,6 +91,7 @@ function findModel(provider: ModelsDevProvider, model: string): ModelsDevModel |
const normalizedEntries = new Map<string, ModelsDevModel>();
for (const [key, value] of Object.entries(models)) {
if (!value || typeof value !== 'object') continue;
normalizedEntries.set(normalizeId(key), value);
if (typeof value.id === 'string') normalizedEntries.set(normalizeId(value.id), value);
}
@@ -188,6 +189,7 @@ export function getKnownModelsDevModels(): string[] {
const ids = new Set<string>();
for (const provider of Object.values(registry)) {
for (const model of Object.values(provider.models ?? {})) {
if (!model || typeof model !== 'object') continue;
if (typeof model.id === 'string') ids.add(`${provider.id}/${model.id}`);
}
}
+20
View File
@@ -392,6 +392,26 @@ describe('model-pricing', () => {
expect(pricing.outputPerMillion).toBe(0);
});
it('ignores malformed models.dev entries during provider-aware lookups', () => {
setCachedModelsDevRegistry({
openai: {
id: 'openai',
name: 'OpenAI',
models: {
bad: null as unknown as never,
'gpt-4o': {
id: 'gpt-4o',
name: 'GPT-4o',
cost: { input: 2.5, output: 10 },
},
},
},
});
expect(() => getModelPricing('gpt-4o', { provider: 'openai' })).not.toThrow();
expect(getModelPricing('gpt-4o', { provider: 'openai' }).inputPerMillion).toBe(2.5);
});
it('prefers provider-aware models.dev pricing over exact static table matches', () => {
const pricing = getModelPricing('gpt-4o', { provider: 'github-copilot' });
expect(pricing.inputPerMillion).toBe(0);