{"record":{"id":"359c22b7caaa7a27","repo":"danny-avila/LibreChat","slug":"this-tool-is-only-available-for-agents-359c22","errorCode":null,"errorMessage":"This tool is only available for agents.","messagePattern":"This tool is only available for agents\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"api/app/clients/tools/structured/OpenAIImageTools.js","lineNumber":68,"sourceCode":"\n/**\n * Creates OpenAI Image tools (generation and editing)\n * @param {Object} fields - Configuration fields\n * @param {ServerRequest} fields.req - Whether the tool is being used in an agent context\n * @param {boolean} fields.isAgent - Whether the tool is being used in an agent context\n * @param {string} fields.IMAGE_GEN_OAI_API_KEY - The OpenAI API key\n * @param {boolean} [fields.override] - Whether to override the API key check, necessary for app initialization\n * @param {MongoFile[]} [fields.imageFiles] - The images to be used for editing\n * @param {string} [fields.imageOutputType] - The image output type configuration\n * @param {string} [fields.fileStrategy] - The file storage strategy\n * @returns {Array<ReturnType<tool>>} - Array of image tools\n */\nfunction createOpenAIImageTools(fields = {}) {\n  /** @type {boolean} Used to initialize the Tool without necessary variables. */\n  const override = fields.override ?? false;\n  /** @type {boolean} */\n  if (!override && !fields.isAgent) {\n    throw new Error('This tool is only available for agents.');\n  }\n  const { req } = fields;\n  const imageOutputType = fields.imageOutputType || EImageOutputType.PNG;\n  const appFileStrategy = fields.fileStrategy;\n\n  const getApiKey = () => {\n    const apiKey = process.env.IMAGE_GEN_OAI_API_KEY ?? '';\n    if (!apiKey && !override) {\n      throw new Error('Missing IMAGE_GEN_OAI_API_KEY environment variable.');\n    }\n    return apiKey;\n  };\n\n  let apiKey = fields.IMAGE_GEN_OAI_API_KEY ?? getApiKey();\n  const closureConfig = { apiKey };\n\n  const imageModel = process.env.IMAGE_GEN_OAI_MODEL || 'gpt-image-1';\n","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/app/clients/tools/structured/OpenAIImageTools.js#L50-L86","documentation":"Construction-time guard in createOpenAIImageTools: the OpenAI image generate/edit tools are agent-only. If `fields.override` is false AND `fields.isAgent` is not truthy, the factory throws before building any tool. Mirrors the same pattern used by GeminiImageGen.","triggerScenarios":"Calling createOpenAIImageTools() from a non-agent code path that forwards fields without `isAgent: true` and is not a manifest/bootstrap call (where override:true is used).","commonSituations":"Wiring OpenAI image tools into a legacy/assistant flow; a refactor that dropped isAgent when forwarding fields; a custom route that builds tools outside the agent controller; unit tests that call the factory without setting either flag.","solutions":["Construct the tools only from the agent tool-loading path, which sets `isAgent: true`.","For app initialization / manifest loading, pass `override: true`.","Use the dedicated non-agent image-generation endpoint for non-agent flows instead of these tools.","Audit field forwarding at the call site to ensure isAgent is preserved."],"exampleFix":"// before\nconst tools = createOpenAIImageTools({ req }); // throws\n\n// after\nconst tools = createOpenAIImageTools({ req, isAgent: true });\n// or bootstrap:\nconst tools = createOpenAIImageTools({ override: true });","handlingStrategy":"validation","validationCode":"function assertAgentFactory(fields = {}) {\n  if (!fields.override && !fields.isAgent) {\n    throw new Error('createOpenAIImageTools requires isAgent:true (or override:true for bootstrap).');\n  }\n}","typeGuard":"function isAgentFactoryCall(fields) {\n  return Boolean(fields?.override || fields?.isAgent);\n}","tryCatchPattern":null,"preventionTips":["Call createOpenAIImageTools only from the agent tool-loading path.","Forward fields unchanged so isAgent is preserved.","Reserve override:true for manifest/bootstrap only."],"tags":["openai-image","agent-only","authorization","tool-factory"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}