{"record":{"id":"9074ade92437f885","repo":"BoundaryML/baml","slug":"cannot-convert-file-media-to-user-facing-media","errorCode":null,"errorMessage":"Cannot convert file media to user facing media","messagePattern":"Cannot convert file media to user facing media","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/language_client_python/src/types/media_repr.rs","lineNumber":61,"sourceCode":"        }\n    }\n}\n\nimpl TryInto<UserFacingBamlMedia> for &BamlMedia {\n    type Error = anyhow::Error;\n\n    fn try_into(self) -> Result<UserFacingBamlMedia> {\n        Ok(UserFacingBamlMedia {\n            mime_type: self.mime_type.clone(),\n            content: match &self.content {\n                BamlMediaContent::Url(url) => UserFacingBamlMediaContent::Url {\n                    url: url.url.clone(),\n                },\n                BamlMediaContent::Base64(base64) => UserFacingBamlMediaContent::Base64 {\n                    base64: base64.base64.clone(),\n                },\n                BamlMediaContent::File(_) => {\n                    anyhow::bail!(\"Cannot convert file media to user facing media\")\n                }\n            },\n        })\n    }\n}\n\n/// This function is used for Pydantic compatibility in three ways:\n///\n///   - allows constructing Pydantic models containing a BAML media instance\n///   - allows FastAPI requests to deserialize BAML media instances in JSON format\n///   - allows serializing BAML media instances in JSON format\n///\n/// Ideally this belongs in baml_py.internal_monkeypatch, so that we can get\n/// ruff-based type checking, but this depends on the pydantic libraries, so we\n/// can't implement this in internal_monkeypatch without adding a hard dependency\n/// on pydantic. And we don't want to do _that_, because that will make it harder\n/// to implement output_type python/vanilla in the future.\n///","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_python/src/types/media_repr.rs#L43-L79","documentation":"When converting a BamlMedia whose content came from a local File into a UserFacingBamlMedia (the representation exposed to users in results), BAML refuses because file-based media has no URL or base64 payload to expose. This is an intentional bail, not an internal bug.","triggerScenarios":"Accessing `.media`/user-facing representation of an image/audio/pdf that was loaded with `baml.markdown`-style file references (BamlMediaContent::File) instead of a URL or inline base64.","commonSituations":"Reading media attached via a file path from a function result and trying to serialize or inspect it as user-facing content; occurs after round-tripping media into a response object.","solutions":["Load media as base64 or URL explicitly instead of a file reference when you need the user-facing form","Read the file yourself and pass base64: media = base64.b64encode(open(path,'rb').read()).decode()","Keep the original BamlMedia object rather than converting to user-facing media"],"exampleFix":"// before\nuser_media = result.media.to_user_facing()  # raises for file media\n// after\nimport base64\nmedia = baml.Image(base64=base64.b64encode(open('img.png','rb').read()).decode())\nuser_media = result.media.to_user_facing()","handlingStrategy":"type-guard","validationCode":"def is_file_media(m):\n    return getattr(m, 'url', None) is None and getattr(m, 'base64', None) is None","typeGuard":"def user_facing_available(media) -> bool:\n    return not (hasattr(media, 'file') and media.file and not media.base64 and not media.url)","tryCatchPattern":"try:\n    uf = media.to_user_facing()\nexcept Exception:\n    uf = load_as_base64(media.file_path)","preventionTips":["Prefer passing media as base64 or URL instead of file references","Guard conversions with a check for file-based content","Keep the raw BamlMedia for round-tripping"],"tags":["media","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}