BoundaryML/baml · error · minijinja::Error

BadSerialization

BadSerialization

Error message

type '{media_type}' is not supported in outputs

What it means

While rendering the output schema for a prompt, BAML encountered a type whose kind is a media type (image, audio, video, pdf). Media types are valid in function inputs but cannot be represented in the text schema shown to the LLM, so the serializer raises BadSerialization. The function's output type must be restricted to primitives, enums, classes, lists, maps, and unions.

Solutions

  1. Change the function's return type to a non-media type (e.g. string, or a class with a url/path string field).
  2. Move the media type to the function inputs; outputs must be serializable text schema types.
  3. If you need binary results, have the LLM return a reference (URL/path) as a string field instead.

Example fix

// before
function ExtractImage() -> image
// after
function ExtractImage() -> string  // or a class with an image_url string field
Defensive patterns

Strategy: validation

Validate before calling

// scan BAML function signatures for media types in outputs before compiling
const MEDIA = ["image","audio","video","pdf"];
const bad = functions.filter(f => MEDIA.some(m => f.outputType.includes(m)));
if (bad.length) throw new Error(`Media types not allowed in outputs: ${bad.map(f=>f.name)}`);

Type guard

const outputHasMediaType = (sig: string) =>
  /->\s*.*(image|audio|video|pdf)\b/.test(sig);

Try / catch

try {
  await renderOutputFormat(fn);
} catch (e) {
  if (String(e).includes("is not supported in outputs")) {
    throw new Error(`Function ${fn.name} must not return media types`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Declaring a BAML function whose return type is or contains `image`, `audio`, `video`, or `pdf` (e.g. `function F() -> image`), which causes output_format rendering to hit TypeValue::Media.

Common situations: Copy-pasting an input type to the output position; wrapping media in unions/classes that appear in outputs; intending the model to 'return an image' without realizing outputs are text schemas.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/81e318368d6e0f00. Report an issue: GitHub.

Appendix: source

Thrown at engine/baml-lib/jinja-runtime/src/output_format/types.rs:699

    /// This function is the entry point for recursive schema rendering.
    ///
    /// Read the documentation of [`Self::render_possibly_hoisted_type`] for
    /// more details.
    fn inner_type_render(
        &self,
        options: &RenderOptions,
        field: &TypeIR,
        render_ctx: &RenderCtx,
    ) -> Result<String, minijinja::Error> {
        Ok(match field {
            TypeIR::Primitive(t, _) => match t {
                TypeValue::String => "string".to_string(),
                TypeValue::Int => "int".to_string(),
                TypeValue::Float => "float".to_string(),
                TypeValue::Bool => "bool".to_string(),
                TypeValue::Null => self.render_null_type(options).to_string(),
                TypeValue::Media(media_type) => {
                    return Err(minijinja::Error::new(
                        minijinja::ErrorKind::BadSerialization,
                        format!("type '{media_type}' is not supported in outputs"),
                    ))
                }
            },
            TypeIR::Literal(v, _) => v.to_string(),
            TypeIR::Enum { name: e, .. } => {
                let Some(enm) = self.enums.get(e) else {
                    return Err(minijinja::Error::new(
                        minijinja::ErrorKind::BadSerialization,
                        format!("Enum {e} not found"),
                    ));
                };

                if render_ctx.hoisted_enums.contains(&enm.name.name) {
                    enm.name.rendered_name().to_string()
                } else {
                    enm.values

View on GitHub (pinned to bd85ce9dee)