{"record":{"id":"731d8cde6cf13708","repo":"BoundaryML/baml","slug":"cannot-convert-media-types-to-json","errorCode":null,"errorMessage":"Cannot convert media types to JSON","messagePattern":"Cannot convert media types to JSON","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/optimize/gepa_runtime.rs","lineNumber":446,"sourceCode":"            Ok(serde_json::Value::Array(items?))\n        }\n        BamlValue::Map(map) => {\n            let obj: Result<serde_json::Map<_, _>> = map\n                .iter()\n                .map(|(k, v)| baml_value_to_json(v).map(|jv| (k.clone(), jv)))\n                .collect();\n            Ok(serde_json::Value::Object(obj?))\n        }\n        BamlValue::Class(_, fields) => {\n            let mut obj = serde_json::Map::new();\n            for (k, v) in fields {\n                obj.insert(k.clone(), baml_value_to_json(v)?);\n            }\n            Ok(serde_json::Value::Object(obj))\n        }\n        BamlValue::Enum(_, val) => Ok(serde_json::Value::String(val.clone())),\n        BamlValue::Media(_) => {\n            anyhow::bail!(\"Cannot convert media types to JSON\")\n        }\n    }\n}\n","sourceCodeStart":428,"sourceCodeEnd":450,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/optimize/gepa_runtime.rs#L428-L450","documentation":"The GEPA optimizer serializes BamlValue test-case data to JSON to build prompts and merge variants. BamlValue::Media (images/audio/PDFs attached to test cases) has no plain-JSON representation the optimizer supports, so baml_value_to_json bails instead of silently dropping or mis-encoding the media. Use of media inputs/outputs is therefore unsupported in optimizer-managed test cases.","triggerScenarios":"Running propose_improvements or merge_variants where a test case's args, labels, or output contains a BamlValue::Media (image/audio/pdf), reached via nested Maps/Lists converted by baml_value_to_json.","commonSituations":"Optimizing a function whose tests pass image/pdf inputs (e.g. OCR or vision tasks); GEPA optimizer runs configured over multimodal test suites; accidentally including a media field in a test-case parameter map.","solutions":["Remove media (image/audio/pdf) values from the test cases used by the optimizer; use text-only fixtures for optimization runs","Point the optimizer at a different, text-only test subset (via test filters/tags)","If the media is small, replace it with a textual description or a base64 string the prompt can consume, adjusting the BAML types accordingly","Restrict multimodal testing to the regular test runner (`baml-cli test`) rather than the GEPA optimizer"],"exampleFix":"// before (test case)\ntest ParseInvoice {\n  args { doc { image \"./invoice.png\" } }\n}\n\n// after (text-only for optimization)\ntest ParseInvoice {\n  args { doc_text \"Invoice #123, total $42.00 ...\" }\n}","handlingStrategy":"fallback","validationCode":"import { BamlValue } from '@baml-lang/core';\nfunction containsMedia(v: any): boolean {\n  if (!v || typeof v !== 'object') return false;\n  if (v.kind === 'media' || v.type === 'media') return true;\n  return Object.values(v).some(containsMedia);\n}\nif (testCases.some((t) => containsMedia(t.args))) throw new Error('Optimizer test set contains media values; use text-only fixtures');","typeGuard":null,"tryCatchPattern":"try {\n  const result = await optimizer.proposeImprovements(testCases);\n} catch (e) {\n  if (/Cannot convert media types/i.test(e.message)) {\n    console.warn('Falling back to text-only test subset for optimization');\n    return optimizer.proposeImprovements(testCases.filter((t) => !containsMedia(t.args)));\n  }\n  throw e;\n}","preventionTips":["Keep optimizer (GEPA) test sets text-only; test multimodal behavior with the regular test runner","Replace media inputs with textual descriptions in optimization datasets","Tag multimodal tests (e.g. #media) and exclude that tag from optimizer filters"],"tags":["json","media","unsupported","optimizer"],"backgroundTag":"unsupported-operation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}