Skip to content

Conversation

@zastrowm
Copy link
Member

@zastrowm zastrowm commented Dec 5, 2025

Description

The OpenAI SDK supports dynamic API key resolution through async functions, enabling use cases like credential rotation, secret manager integration, and per-request authentication. However, our SDK currently only accepts static strings for the apiKey parameter, preventing users from leveraging these capabilities.

Public API Changes

The OpenAIModelOptions.apiKey parameter now accepts either a string or an async function:

// Before: only string supported
const model = new OpenAIModel({
  modelId: 'gpt-4o',
  apiKey: 'sk-...'
})

// After: function also supported
const model = new OpenAIModel({
  modelId: 'gpt-4o',
  apiKey: async () => await secretManager.getApiKey()
})

The change is backward compatible—all existing string-based usage continues to work without modification. The function receives no arguments and must return a Promise<string>. The OpenAI SDK invokes the function before each request and validates the returned value.

Related Issues

None; noticed this when refactoring integration tests

Agent developed via zastrowm#5 and zastrowm#4

Documentation PR

N/A

Type of Change

Bug fix/New feature

Testing

How have you tested the change?

  • I ran npm run check

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

strands-agent and others added 2 commits December 5, 2025 09:07
- Add ApiKeySetter type import from 'openai/client'
- Update OpenAIModelOptions.apiKey type to accept string | ApiKeySetter
- Update TSDoc to document function-based API key usage
- Update error message to mention 'string or function' option
- Add tests for function-based API key scenarios

This enables dynamic API key management for use cases like:
- API key rotation
- Runtime credential refresh
- Fetching keys from secret managers

Resolves: #4
@zastrowm zastrowm merged commit e282048 into strands-agents:main Dec 5, 2025
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants