{"record":{"id":"6f115b865913acc7","repo":"vercel/ai","slug":"description-api-key-must-be-a-string-the-value-6f115b","errorCode":null,"errorMessage":"${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.","messagePattern":"(.+?) API key must be a string\\. The value of the (.+?) environment variable is not a string\\.","errorType":"exception","errorClass":"LoadAPIKeyError","httpStatus":null,"severity":"error","filePath":"packages/provider-utils/src/load-api-key.ts","lineNumber":39,"sourceCode":"    });\n  }\n\n  if (typeof process === 'undefined') {\n    throw new LoadAPIKeyError({\n      message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables are not supported in this environment.`,\n    });\n  }\n\n  apiKey = process.env[environmentVariableName];\n\n  if (apiKey == null) {\n    throw new LoadAPIKeyError({\n      message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`,\n    });\n  }\n\n  if (typeof apiKey !== 'string') {\n    throw new LoadAPIKeyError({\n      message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`,\n    });\n  }\n\n  return apiKey;\n}\n","sourceCodeStart":21,"sourceCodeEnd":46,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/provider-utils/src/load-api-key.ts#L21-L46","documentation":"loadApiKey resolved an API key from the environment variable, but the value stored there is not a string (e.g. a number or object leaked in through a mocked/patched process.env). It throws LoadAPIKeyError to stop a non-string credential being used.","triggerScenarios":"process.env[environmentVariableName] is set to a non-string value — typically in tests where process.env was assigned non-string values, or in runtimes that type env entries loosely.","commonSituations":"Unit tests assigning process.env.OPENAI_API_KEY = 12345; mocked env objects in test setup; scripts that patch process.env with parsed config objects.","solutions":["Fix whatever assigned the non-string value: env vars must be strings (String(value))","In tests, set process.env values as strings (or use vi.stubEnv with a string)","Pass the key explicitly as a string instead of relying on the environment","Check for config loaders that write parsed objects/numbers into process.env"],"exampleFix":"// before (test setup)\nprocess.env.OPENAI_API_KEY = 12345;\n// after\nprocess.env.OPENAI_API_KEY = 'sk-test-12345';","handlingStrategy":"validation","validationCode":"const raw = process.env.OPENAI_API_KEY;\nif (raw != null && typeof raw !== 'string') throw new Error('OPENAI_API_KEY env value must be a string');\nconst provider = createOpenAI({ apiKey: raw });","typeGuard":"function isStringEnvValue(value: unknown): value is string {\n  return typeof value === 'string';\n}","tryCatchPattern":"try {\n  const provider = createOpenAI();\n} catch (error) {\n  if (/environment variable is not a string/.test(String(error.message))) {\n    throw new Error('process.env was patched with non-string values — check test setup/config loader');\n  }\n  throw error;\n}","preventionTips":["In tests use vi.stubEnv('OPENAI_API_KEY', 'sk-test') or assign strings only","Never assign parsed config objects/numbers into process.env","Assert env values are strings in test setup utilities"],"tags":["api-key","environment","testing","validation"],"backgroundTag":"invalid-api-key","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}