{"record":{"id":"c8cf0d4501c671b1","repo":"HeyPuter/puter","slug":"bad-request-c8cf0d","errorCode":"bad_request","errorMessage":"message is not allowed","messagePattern":"message is not allowed","errorType":"http","errorClass":"HttpError","httpStatus":400,"severity":"error","filePath":"src/backend/drivers/ai-chat/utils/OpenAIUtil.js","lineNumber":478,"sourceCode":"            completion,\n            usage_calculator,\n        });\n\n        return {\n            stream: true,\n            init_chat_stream,\n            finally_fn,\n        };\n    }\n\n    if (finally_fn) await finally_fn();\n\n    // We need to moderate the completion too\n    const mod_text = completion.choices[0].message.content;\n    if (moderate && mod_text !== null) {\n        const moderation_result = await moderate(mod_text);\n        if (moderation_result.flagged) {\n            throw new HttpError(400, 'message is not allowed', {\n                legacyCode: 'bad_request',\n            });\n        }\n    }\n\n    const ret = completion.choices[0];\n    const completion_usage = deviations.coerce_completion_usage(completion);\n    ret.usage = usage_calculator\n        ? usage_calculator({\n              ...completion,\n              usage: completion_usage,\n          })\n        : {\n              input_tokens: completion_usage.prompt_tokens,\n              output_tokens: completion_usage.completion_tokens,\n          };\n    return ret;\n};","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/HeyPuter/puter/blob/908ec23eda38526170322c3edf71ba45ecb1ca95/src/backend/drivers/ai-chat/utils/OpenAIUtil.js#L460-L496","documentation":"Thrown by the non-streaming chat-completion path in handle_completion_output after the model returns. Puter runs the model's own assistant content through its moderation function; if moderation flags it (moderation_result.flagged === true), the response is rejected with HTTP 400. It is content-policy enforcement on the completion, not on the user's prompt.","triggerScenarios":"A non-streaming AI chat call (puter.ai.chat with stream:false, or any provider routed through handle_completion_output) whose model output contains text the moderation layer considers disallowed.","commonSituations":"The model produces output touching violence, sexual content, hate, self-harm, or other policy categories the configured moderator screens for; or a mis-configured/over-aggressive moderator flags benign output.","solutions":["Rephrase or constrain the conversation so the model is unlikely to emit flagged content (tighten the system prompt).","Inspect the moderator configuration/ thresholds if benign completions are being flagged.","If streaming is acceptable, use stream:true — note moderation still applies but surfaces differently; the 400 here is the non-stream path.","Catch the 400 'message is not allowed' client-side and show the user a content-policy message instead of crashing."],"exampleFix":"// before\nconst resp = await puter.ai.chat('write something edgy');\n// after — guard the policy error\ntry {\n  const resp = await puter.ai.chat(prompt, { stream: false });\n} catch (e) {\n  if (e.code === 'bad_request' && /not allowed/.test(e.message)) {\n    showUser('Response blocked by content policy.');\n    return;\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Pre-screen the user prompt only; you cannot pre-predict model-output moderation.\n// Validate prompt is a non-empty string before the call:\nif (typeof prompt !== 'string' || prompt.trim() === '') {\n  throw new Error('prompt required');\n}","typeGuard":null,"tryCatchPattern":"try {\n  const resp = await puter.ai.chat(prompt, { stream: false });\n} catch (e) {\n  if (e?.code === 'bad_request' && /not allowed/i.test(e?.message)) {\n    showContentPolicyNotice();\n    return;\n  }\n  throw e;\n}","preventionTips":["Keep the system prompt tightly scoped to reduce the chance of flagged completions.","Catch bad_request / 'not allowed' specifically and show a policy notice rather than crashing.","If benign output is repeatedly flagged, review the moderator configuration."],"tags":["ai-chat","moderation","content-policy","bad-request"],"backgroundTag":null,"analyzedSha":"908ec23eda38526170322c3edf71ba45ecb1ca95","analyzedAt":"2026-08-12T20:53:15.911Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}