{"record":{"id":"8d79b460ae0d1961","repo":"siyuan-note/siyuan","slug":"image-mime-type-is-missing","errorCode":null,"errorMessage":"image MIME type is missing","messagePattern":"image MIME type is missing","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/attachments.go","lineNumber":146,"sourceCode":"\tif imageCount == 0 {\n\t\treturn openai.ChatCompletionMessage{}, false\n\t}\n\tif omitted {\n\t\tparts = append(parts, openai.ChatMessagePart{\n\t\t\tType: openai.ChatMessagePartTypeText,\n\t\t\tText: \"One or more image attachments were omitted because they were unavailable or exceeded the request limit.\",\n\t\t})\n\t}\n\treturn openai.ChatCompletionMessage{\n\t\tRole:         openai.ChatMessageRoleUser,\n\t\tMultiContent: parts,\n\t}, true\n}\n\nfunc resolveAgentAttachment(attachment AgentAttachment) (data []byte, mimeType string, width, height int, err error) {\n\tif len(attachment.Data) > 0 {\n\t\tif attachment.MIMEType == \"\" {\n\t\t\terr = fmt.Errorf(\"image MIME type is missing\")\n\t\t\treturn\n\t\t}\n\t\tdata = attachment.Data\n\t\tmimeType = attachment.MIMEType\n\t\twidth = attachment.Width\n\t\theight = attachment.Height\n\t\treturn\n\t}\n\tvar prepared kernelModel.PreparedDocumentImage\n\tvar prepareErr error\n\tif attachment.DocumentID == \"\" {\n\t\tprepared, prepareErr = kernelModel.PrepareAgentMessageImage(attachment.Path)\n\t} else {\n\t\tprepared, prepareErr = kernelModel.PrepareDocumentImage(attachment.DocumentID, attachment.Path)\n\t}\n\tif prepareErr != nil {\n\t\terr = prepareErr\n\t\treturn","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/agent/attachments.go#L128-L164","documentation":"Raised by resolveAgentAttachment when an image attachment carries inline Data but an empty MIMEType. The image content protocol requires an explicit MIME type to build the multimodal content part; without it the data URL/content type cannot be constructed, so the attachment is rejected with this error instead of guessing the format.","triggerScenarios":"In attachments.go (~line 146): an AgentAttachment has len(attachment.Data) > 0 but attachment.MIMEType == \"\" — e.g. an attachment constructed programmatically or read from storage that lost its MIME metadata — and is then resolved by buildAttachmentMessage for inclusion in the model request.","commonSituations":"A plugin or script creates AgentAttachment objects manually and forgets to set MIMEType; an older saved attachment record predates the MIMEType field and unmarshals as empty; a data-import path copies Data but drops metadata.","solutions":["Set attachment.MIMEType (e.g. \"image/png\", \"image/jpeg\") whenever constructing or loading an AgentAttachment with inline Data.","If loading legacy records without MIMEType, detect the type from the image bytes (magic-number sniffing) before resolving.","Validate attachments on ingest (reject empty MIMEType early) rather than at request-build time.","Re-attach the image through the normal UI path so MIME metadata is populated."],"exampleFix":"// before\nattachment := AgentAttachment{Type: \"image\", Data: pngBytes}\n// after\nattachment := AgentAttachment{Type: \"image\", Data: pngBytes, MIMEType: \"image/png\"}","handlingStrategy":"validation","validationCode":"function hasMime(a) { return a.type !== \"image\" || !a.data?.length || Boolean(a.mimeType) }","typeGuard":"func hasImageMIME(a AgentAttachment) bool {\n    return a.Type != \"image\" || len(a.Data) == 0 || a.MIMEType != \"\"\n}","tryCatchPattern":"data, mime, w, h, err := resolveAgentAttachment(att)\nif err != nil {\n    // sniff MIME from bytes (magic number) and retry once, else drop the attachment\n}","preventionTips":["Always set MIMEType when constructing AgentAttachment values with inline Data.","Validate attachments at ingest time, not at request-build time.","Sniff MIME from image bytes when loading records missing metadata.","Re-create attachments through the standard UI path so metadata is populated."],"tags":["attachments","mime","validation","agent"],"backgroundTag":"empty-required-field","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}