Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Passing Custom HTTP Headers in generateText and streamText Tool Execution #3465

Open
Slyracoon23 opened this issue Nov 4, 2024 · 0 comments
Labels
ai/core enhancement New feature or request

Comments

@Slyracoon23
Copy link

Feature Description

This feature request proposes the ability to pass custom HTTP headers in generateText and streamText calls. Allowing developers to add arbitrary headers supports scenarios requiring authentication, tracking, or other custom headers needed for secure and flexible API interactions within tools.

Example Implementation

The headers would be set in the generateText or streamText options and passed automatically to each tool’s execute function, if defined. Here’s a sample setup:

import { generateText, streamText } from 'ai';
import { z } from 'zod';

const tools = {
  fetchData: {
    description: 'Fetches data with custom headers.',
    parameters: z.object({
      dataId: z.string(),
    }),
    async execute(args: { dataId: string }, { abortSignal, headers }: { abortSignal: AbortSignal, headers?: Record<string, string> }) {
      const response = await fetch(`https://api.example.com/data/${args.dataId}`, {
        method: 'GET',
        headers: {
          Authorization: headers?.Authorization || '',
          'X-Custom-Header': headers?.['X-Custom-Header'] || '',
        },
        signal: abortSignal,
      });
      const data = await response.json();
      return data;
    },
  },
};

// Usage with generateText
const { text, toolResults } = await generateText({
  model: openai('gpt-4-turbo'),
  prompt: 'Fetch data for specific ID',
  tools,
  headers: { 
    Authorization: 'Bearer YOUR_TOKEN',
    'X-Custom-Header': 'CustomValue',
  },
});

// Usage with streamText
const stream = await streamText({
  model: openai('gpt-4-turbo'),
  prompt: 'Stream data for specific ID',
  tools,
  headers: {
    Authorization: 'Bearer YOUR_TOKEN',
    'X-Custom-Header': 'CustomValue',
  },
});

Use Case

This feature is beneficial for applications that need to interact with third-party services requiring specific headers, such as API keys, OAuth tokens, or custom tracking information. It enhances the SDK’s flexibility, allowing developers to manage headers dynamically within a single generateText or streamText call. This is especially useful for complex applications using multiple authenticated API calls.

Additional context

Currently, developers must manually handle headers within each tool’s execute function, which can lead to redundant code and maintenance challenges. By centralizing header configuration in generateText and streamText, this feature would streamline the process of making authenticated or custom requests within tool executions.

@lgrammel lgrammel added enhancement New feature or request ai/core labels Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants