Skip to content

Conversation

@matthewmichel
Copy link

@matthewmichel matthewmichel commented Mar 12, 2025

Extending the openai-tools to support the "file_search" tool call documented by OpenAI here: https://platform.openai.com/docs/guides/tools-file-search?lang=javascript

Note: originally set this up as "file_search_preview", but opted for "file_search" because OpenAI does not have this tool tagged as a preview feature and the tool name in the OpenAI docs is "file_search".

@lgrammel
Copy link
Collaborator

Nice! without parsing their special outputs how will this work in practice?

@matthewmichel
Copy link
Author

When I tested it to see what would happen when adding in a vector_store_id it returned a response as expected through the "text" value with references to the vector store files (indicating to me that it worked to perform the file search).

The "text" value (returned by the new OpenAI Responses API) should work as expected when using the file_search tool. I'll work on adding some handlers for the new "annotations" value that is returned by the Responses API for better clarity.

@matthewmichel
Copy link
Author

matthewmichel commented Mar 12, 2025

@lgrammel I should have checked before responding. The "annotations" property is already available on the OpenAIResponsesAssistantMessage. This should be enough to pass through those annotations through the SDK response.

export type OpenAIResponsesAssistantMessage = {
  role: 'assistant';
  content: Array<{ 
    type: 'output_text'; 
    text: string; 
    annotations?: Array<{
      type: 'file_citation';
      index: number;
      file_id: string;
      filename: string;
    } | {
      type: 'url_citation';
      start_index: number;
      end_index: number;
      url: string;
      title: string;
    }>;
  }>;
};

This matches the OpenAI docs for the file search tool: https://platform.openai.com/docs/guides/tools-file-search

EDIT: I didn't realize I actually made this change 🤦‍♂️ The update to this type has been pushed to this PR.

@matthewmichel
Copy link
Author

Ok made some adjustments to ensure the annotations information is being returned properly through the sdk. Tested this locally and got a nice sources value when trying out with the file_search tool.

sources: [
    {
      sourceType: 'file',
      id: 'PSvUjMxv2hlJ4vuj',
      fileId: 'file-IhP983o4sPmYkgGOtVdureJO',
      filename: 'Artificial Intelligence (AI) Policy.txt'
    }
  ]

I've also updated the next-openai examples to account for either url or file source annotations.

Comment on lines +33 to +58
| {
/**
* A file source. This is returned by file search RAG models.
*/
sourceType: 'file';

/**
* The ID of the source.
*/
id: string;

/**
* The ID of the file.
*/
fileId: string;

/**
* The name of the file.
*/
filename: string;

/**
* Additional provider metadata for the source.
*/
providerMetadata?: LanguageModelV1ProviderMetadata;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a potentially critical change because it also needs to work for anthropic and google. i would need more time to evaluate, most likely want to do this myself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your PR for the computer use tool. Would you like me to adjust this PR to follow those same practices for this file_search tool?

@cosbgn
Copy link

cosbgn commented Mar 13, 2025

Any chance we could get this release as experimental_alpha and then migrate to one which supports all providers? I would love to migrate a lot of Assistants to the new responses api but I'm blocked by the file_search limitation

@MwSpaceLLC
Copy link

Waiting for this file_search adapter,

A game changer for ALL 🙂‍↔️

@IKatsuba
Copy link

This PR will close #5188

@nsenthilkumar
Copy link

Is there any update on this @lgrammel ?

@shrix1
Copy link

shrix1 commented Apr 15, 2025

Hey, @lgrammel any update ?

@kvnwolf
Copy link
Contributor

kvnwolf commented May 6, 2025

@lgrammel sorry for the possible spam, but any plans to merge this soon? Any change needed to the PR?

@andresgutgon
Copy link

Looking forward to this be merged 💪

@ruidpm
Copy link

ruidpm commented May 22, 2025

@lgrammel any updates on this?

@lagorsse
Copy link

seems like blocked, I really need this tool, I could use assistant to move this into openai could work for some of you, but it doesn't work for me as I am trying to use assistant ui cloud which doesn't support assistant.

@Jaikant
Copy link
Contributor

Jaikant commented Jun 7, 2025

@lgrammel do you need any help on this? It seems the responses api is not compatible with APIs from Anthropic/xAI especially these custom constructs like file_search, web_search in the responses API?

https://apimagic.ai/blog/switching-assistant-responses-api

We can either use the existing tools parameter available on the ai-sdk methods or add it to the providerOptions like below:

const providerOptions = {
  openai: {
    tools: [
      {
        type: 'file_search',
        vector_store_ids: ['vs_1234567890'], 
        max_num_results: 20,
      },
    ],
    tool_choice: { type: 'tool', tool_name: 'file_search' },
  },
};

@ssg-chris
Copy link

Any news?

@lgrammel
Copy link
Collaborator

This functionality is available in ai sdk 5 beta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.