Skip to content

An AI-powered search engine with a generative UI

Fix schema selection for o-prefix models in OpenAI and Azure providers

Yoshiki Miura285b3c554374
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index 06d76755..a2806d51 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -1,8 +1,8 @@
import { CoreMessage, smoothStream, streamText } from 'ai'
-import { askQuestionTool } from '../tools/question'
+import { createQuestionTool } from '../tools/question'
import { retrieveTool } from '../tools/retrieve'
-import { searchTool } from '../tools/search'
-import { videoSearchTool } from '../tools/video-search'
+import { createSearchTool } from '../tools/search'
+import { createVideoSearchTool } from '../tools/video-search'
import { getModel } from '../utils/registry'
const SYSTEM_PROMPT = `
@@ -47,6 +47,11 @@ export function researcher({
try {
const currentDate = new Date().toLocaleString()
+ // Create model-specific tools
+ const searchTool = createSearchTool(model)
+ const videoSearchTool = createVideoSearchTool(model)
+ const askQuestionTool = createQuestionTool(model)
+
return {
model: getModel(model),
system: `${SYSTEM_PROMPT}\nCurrent date and time: ${currentDate}`,
diff --git a/lib/schema/question.ts b/lib/schema/question.ts
new file mode 100644
index 00000000..1b9c0d70
--- /dev/null
+++ b/lib/schema/question.ts
@@ -0,0 +1,48 @@
+import { z } from 'zod'
+
+// Standard schema with optional fields for inputLabel and inputPlaceholder
+export const questionSchema = z.object({
+ question: z.string().describe('The main question to ask the user'),
+ options: z
+ .array(
+ z.object({
+ value: z.string().describe('Option identifier (always in English)'),
+ label: z.string().describe('Display text for the option')
+ })
+ )
+ .describe('List of predefined options'),
+ allowsInput: z.boolean().describe('Whether to allow free-form text input'),
+ inputLabel: z.string().optional().describe('Label for free-form input field'),
+ inputPlaceholder: z
+ .string()
+ .optional()
+ .describe('Placeholder text for input field')
+})
+
+// Strict schema with all fields required, for specific models like o3-mini
+export const strictQuestionSchema = z.object({
+ question: z.string().describe('The main question to ask the user'),
+ options: z
+ .array(
+ z.object({
+ value: z.string().describe('Option identifier (always in English)'),
+ label: z.string().describe('Display text for the option')
+ })
+ )
+ .describe('List of predefined options'),
+ allowsInput: z.boolean().describe('Whether to allow free-form text input'),
+ inputLabel: z.string().describe('Label for free-form input field'),
+ inputPlaceholder: z.string().describe('Placeholder text for input field')
+})
+
+/**
+ * Returns the appropriate question schema based on the full model name.
+ * Uses the strict schema for OpenAI models starting with 'o'.
+ */
+export function getQuestionSchemaForModel(fullModel: string) {
+ const [provider, modelName] = fullModel?.split(':') ?? []
+ const useStrictSchema =
+ (provider === 'openai' || provider === 'azure') &&
+ modelName?.startsWith('o')
+ return useStrictSchema ? strictQuestionSchema : questionSchema
+}
diff --git a/lib/schema/search.tsx b/lib/schema/search.tsx
index 63580f65..19283dd1 100644
--- a/lib/schema/search.tsx
+++ b/lib/schema/search.tsx
@@ -27,4 +27,44 @@ export const searchSchema = z.object({
)
})
+// Strict schema with all fields required
+export const strictSearchSchema = z.object({
+ query: z.string().describe('The query to search for'),
+ max_results: z
+ .number()
+ .describe('The maximum number of results to return. default is 20'),
+ search_depth: z
+ .enum(['basic', 'advanced'])
+ .describe('The depth of the search'),
+ include_domains: z
+ .array(z.string())
+ .describe(
+ 'A list of domains to specifically include in the search results. Default is None, which includes all domains.'
+ ),
+ exclude_domains: z
+ .array(z.string())
+ .describe(
+ "A list of domains to specifically exclude from the search results. Default is None, which doesn't exclude any domains."
+ )
+})
+
+/**
+ * Returns the appropriate search schema based on the full model name.
+ * Uses the strict schema for OpenAI models starting with 'o'.
+ */
+export function getSearchSchemaForModel(fullModel: string) {
+ const [provider, modelName] = fullModel?.split(':') ?? []
+ const useStrictSchema =
+ (provider === 'openai' || provider === 'azure') &&
+ modelName?.startsWith('o')
+
+ // Ensure search_depth is an enum for the strict schema
+ if (useStrictSchema) {
+ return strictSearchSchema
+ } else {
+ // For the standard schema, keep search_depth as optional string
+ return searchSchema
+ }
+}
+
export type PartialInquiry = DeepPartial<typeof searchSchema>
diff --git a/lib/tools/question.ts b/lib/tools/question.ts
index c913154a..669b86ba 100644
--- a/lib/tools/question.ts
+++ b/lib/tools/question.ts
@@ -1,28 +1,17 @@
+import { getQuestionSchemaForModel } from '@/lib/schema/question'
import { tool } from 'ai'
-import { z } from 'zod'
-export const askQuestionTool = tool({
- description:
- 'Ask a clarifying question with multiple options when more information is needed',
- parameters: z.object({
- question: z.string().describe('The main question to ask the user'),
- options: z
- .array(
- z.object({
- value: z.string().describe('Option identifier (always in English)'),
- label: z.string().describe('Display text for the option')
- })
- )
- .describe('List of predefined options'),
- allowsInput: z.boolean().describe('Whether to allow free-form text input'),
- inputLabel: z
- .string()
- .optional()
- .describe('Label for free-form input field'),
- inputPlaceholder: z
- .string()
- .optional()
- .describe('Placeholder text for input field')
+/**
+ * Creates a question tool with the appropriate schema for the specified model.
+ */
+export function createQuestionTool(fullModel: string) {
+ return tool({
+ description:
+ 'Ask a clarifying question with multiple options when more information is needed',
+ parameters: getQuestionSchemaForModel(fullModel)
+ // execute function removed to enable frontend confirmation
})
- // execute function removed to enable frontend confirmation
-})
+}
+
+// Default export for backward compatibility, using a default model
+export const askQuestionTool = createQuestionTool('openai:gpt-4o-mini')
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index 0291e9e6..5261ef62 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -1,4 +1,4 @@
-import { searchSchema } from '@/lib/schema/search'
+import { getSearchSchemaForModel } from '@/lib/schema/search'
import {
SearchResultImage,
SearchResultItem,
@@ -10,82 +10,100 @@ import { sanitizeUrl } from '@/lib/utils'
import { tool } from 'ai'
import Exa from 'exa-js'
-export const searchTool = tool({
- description: 'Search the web for information',
- parameters: searchSchema,
- execute: async ({
- query,
- max_results = 20,
- search_depth = 'basic',
- include_domains = [],
- exclude_domains = []
- }) => {
- // Tavily API requires a minimum of 5 characters in the query
- const filledQuery =
- query.length < 5 ? query + ' '.repeat(5 - query.length) : query
- let searchResult: SearchResults
- const searchAPI =
- (process.env.SEARCH_API as 'tavily' | 'exa' | 'searxng') || 'tavily'
+/**
+ * Creates a search tool with the appropriate schema for the given model.
+ */
+export function createSearchTool(fullModel: string) {
+ return tool({
+ description: 'Search the web for information',
+ parameters: getSearchSchemaForModel(fullModel),
+ execute: async ({
+ query,
+ max_results = 20,
+ search_depth = 'basic', // Default for standard schema
+ include_domains = [],
+ exclude_domains = []
+ }) => {
+ // Ensure max_results is at least 10
+ const minResults = 10
+ const effectiveMaxResults = Math.max(
+ max_results || minResults,
+ minResults
+ )
+ const effectiveSearchDepth = search_depth as 'basic' | 'advanced'
- const effectiveSearchDepth =
- searchAPI === 'searxng' &&
- process.env.SEARXNG_DEFAULT_DEPTH === 'advanced'
- ? 'advanced'
- : search_depth || 'basic'
+ // Tavily API requires a minimum of 5 characters in the query
+ const filledQuery =
+ query.length < 5 ? query + ' '.repeat(5 - query.length) : query
+ let searchResult: SearchResults
+ const searchAPI =
+ (process.env.SEARCH_API as 'tavily' | 'exa' | 'searxng') || 'tavily'
- console.log(
- `Using search API: ${searchAPI}, Search Depth: ${effectiveSearchDepth}`
- )
+ const effectiveSearchDepthForAPI =
+ searchAPI === 'searxng' &&
+ process.env.SEARXNG_DEFAULT_DEPTH === 'advanced'
+ ? 'advanced'
+ : effectiveSearchDepth || 'basic'
- try {
- if (searchAPI === 'searxng' && effectiveSearchDepth === 'advanced') {
- // API route for advanced SearXNG search
- const baseUrl =
- process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'
- const response = await fetch(`${baseUrl}/api/advanced-search`, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- query: filledQuery,
- maxResults: max_results,
- searchDepth: effectiveSearchDepth,
- includeDomains: include_domains,
- excludeDomains: exclude_domains
+ console.log(
+ `Using search API: ${searchAPI}, Search Depth: ${effectiveSearchDepthForAPI}`
+ )
+
+ try {
+ if (
+ searchAPI === 'searxng' &&
+ effectiveSearchDepthForAPI === 'advanced'
+ ) {
+ const baseUrl =
+ process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'
+ const response = await fetch(`${baseUrl}/api/advanced-search`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ query: filledQuery,
+ maxResults: effectiveMaxResults,
+ searchDepth: effectiveSearchDepthForAPI,
+ includeDomains: include_domains,
+ excludeDomains: exclude_domains
+ })
})
- })
- if (!response.ok) {
- throw new Error(
- `Advanced search API error: ${response.status} ${response.statusText}`
+ if (!response.ok) {
+ throw new Error(
+ `Advanced search API error: ${response.status} ${response.statusText}`
+ )
+ }
+ searchResult = await response.json()
+ } else {
+ searchResult = await (searchAPI === 'tavily'
+ ? tavilySearch
+ : searchAPI === 'exa'
+ ? exaSearch
+ : searxngSearch)(
+ filledQuery,
+ effectiveMaxResults,
+ effectiveSearchDepthForAPI,
+ include_domains,
+ exclude_domains
)
}
- searchResult = await response.json()
- } else {
- searchResult = await (searchAPI === 'tavily'
- ? tavilySearch
- : searchAPI === 'exa'
- ? exaSearch
- : searxngSearch)(
- filledQuery,
- max_results,
- effectiveSearchDepth === 'advanced' ? 'advanced' : 'basic',
- include_domains,
- exclude_domains
- )
- }
- } catch (error) {
- console.error('Search API error:', error)
- searchResult = {
- results: [],
- query: filledQuery,
- images: [],
- number_of_results: 0
+ } catch (error) {
+ console.error('Search API error:', error)
+ searchResult = {
+ results: [],
+ query: filledQuery,
+ images: [],
+ number_of_results: 0
+ }
}
+
+ console.log('completed search')
+ return searchResult
}
+ })
+}
- console.log('completed search')
- return searchResult
- }
-})
+// Default export for backward compatibility, using a default model
+export const searchTool = createSearchTool('openai:gpt-4o-mini')
export async function search(
query: string,
diff --git a/lib/tools/video-search.ts b/lib/tools/video-search.ts
index a500d510..89834eaf 100644
--- a/lib/tools/video-search.ts
+++ b/lib/tools/video-search.ts
@@ -1,28 +1,36 @@
+import { getSearchSchemaForModel } from '@/lib/schema/search'
import { tool } from 'ai'
-import { searchSchema } from '@/lib/schema/search'
-export const videoSearchTool = tool({
- description: 'Search for videos from YouTube',
- parameters: searchSchema,
- execute: async ({ query }) => {
- try {
- const response = await fetch('https://google.serper.dev/videos', {
- method: 'POST',
- headers: {
- 'X-API-KEY': process.env.SERPER_API_KEY || '',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({ q: query })
- })
+/**
+ * Creates a video search tool with the appropriate schema for the model.
+ */
+export function createVideoSearchTool(fullModel: string) {
+ return tool({
+ description: 'Search for videos from YouTube',
+ parameters: getSearchSchemaForModel(fullModel),
+ execute: async ({ query }) => {
+ try {
+ const response = await fetch('https://google.serper.dev/videos', {
+ method: 'POST',
+ headers: {
+ 'X-API-KEY': process.env.SERPER_API_KEY || '',
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({ q: query })
+ })
- if (!response.ok) {
- throw new Error('Network response was not ok')
- }
+ if (!response.ok) {
+ throw new Error('Network response was not ok')
+ }
- return await response.json()
- } catch (error) {
- console.error('Video Search API error:', error)
- return null
+ return await response.json()
+ } catch (error) {
+ console.error('Video Search API error:', error)
+ return null
+ }
}
- }
-})
+ })
+}
+
+// Default export for backward compatibility, using a default model
+export const videoSearchTool = createVideoSearchTool('openai:gpt-4o-mini')