Skip to content

An AI-powered search engine with a generative UI

fix(search): stop passing Tavily's unused result fields to the model (#1037)

Yoshiki Miura9186fbe674d8
diff --git a/lib/tools/search/providers/__tests__/tavily-projection.test.ts b/lib/tools/search/providers/__tests__/tavily-projection.test.ts
new file mode 100644
index 00000000..7c6d9d6b
--- /dev/null
+++ b/lib/tools/search/providers/__tests__/tavily-projection.test.ts
@@ -0,0 +1,69 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { TavilySearchProvider } from '../tavily'
+
+describe('TavilySearchProvider result projection', () => {
+ afterEach(() => {
+ vi.unstubAllEnvs()
+ vi.unstubAllGlobals()
+ })
+
+ const searchWithPayload = async (payload: unknown) => {
+ vi.stubEnv('TAVILY_API_KEY', 'test-key')
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () => Response.json(payload))
+ )
+
+ return new TavilySearchProvider().search('a test query', 5, 'basic')
+ }
+
+ it('keeps only the declared result fields', async () => {
+ const result = await searchWithPayload({
+ query: 'a test query',
+ response_time: 1.2,
+ request_id: 'req_1',
+ results: [
+ {
+ title: 'A',
+ url: 'https://a.test',
+ content: 'alpha',
+ score: 0.93,
+ raw_content: null,
+ id: 'res_1',
+ images: [
+ { url: 'https://a.test/1.png', description: null, score: 10 }
+ ]
+ }
+ ],
+ images: []
+ })
+
+ expect(result.results).toEqual([
+ { title: 'A', url: 'https://a.test', content: 'alpha' }
+ ])
+ expect(result.number_of_results).toBe(1)
+ expect(result).not.toHaveProperty('response_time')
+ expect(result).not.toHaveProperty('request_id')
+ })
+
+ it('still returns top-level images for the UI and the image prompt', async () => {
+ const result = await searchWithPayload({
+ query: 'a test query',
+ results: [{ title: 'A', url: 'https://a.test', content: 'alpha' }],
+ images: [
+ { url: 'https://a.test/1.png', title: 'A', description: 'one' },
+ { url: 'https://a.test/2.png', description: '' }
+ ]
+ })
+
+ expect(result.images).toEqual([
+ {
+ url: 'https://a.test/1.png',
+ description: 'one',
+ title: 'A',
+ sourceUrl: 'https://a.test'
+ }
+ ])
+ })
+})
diff --git a/lib/tools/search/providers/tavily.ts b/lib/tools/search/providers/tavily.ts
index 77a36327..b7f19d21 100644
--- a/lib/tools/search/providers/tavily.ts
+++ b/lib/tools/search/providers/tavily.ts
@@ -1,4 +1,4 @@
-import { SearchResults } from '@/lib/types'
+import { SearchResultItem, SearchResults } from '@/lib/types'
import { sanitizeUrl } from '@/lib/utils'
import { BaseSearchProvider } from './base'
@@ -176,9 +176,23 @@ export class TavilySearchProvider extends BaseSearchProvider {
)
: data.images.map((url: string) => sanitizeUrl(url))
+ const results: SearchResultItem[] = (
+ (data.results ?? []) as Array<{
+ title?: string
+ url?: string
+ content?: string
+ }>
+ ).map(result => ({
+ title: result.title ?? '',
+ url: result.url ?? '',
+ content: result.content ?? ''
+ }))
+
return {
- ...data,
- images: processedImages
+ results,
+ images: processedImages,
+ query: data.query ?? filledQuery,
+ number_of_results: results.length
}
}
}