BoundaryML/baml · error

baml.fetch_as: expected type parameter to be a Baml type…

Error message

baml.fetch_as: expected type parameter to be a Baml type, got {}

What it means

The second argument of `baml.fetch_as` must be a BAML type (a BamlType IR object). The VM looked up the value and found a non-type object (a value instead of a type), so it cannot build an output format to parse the response.

Solutions

  1. Pass a BAML type identifier (e.g. `MyType`) as the second argument, not a value or string
  2. Verify the type is declared in the .baml file and visible at the call site
  3. If the type comes from a variable, ensure it was resolved to a type at compile time

Example fix

// before (BAML)
let data = baml.fetch_as("https://api.example.com/data", "MyType")
// after
let data = baml.fetch_as("https://api.example.com/data", MyType)
Defensive patterns

Strategy: type-guard

Validate before calling

// ensure the second argument is a type reference (capitalized identifier), not a value
function isTypeReference(arg) {
  return typeof arg === 'string' || typeof arg === 'object'
    ? /^[A-Z][A-Za-z0-9_]*$/.test(String(arg))
    : false;
}

Type guard

function isBamlTypeRef(tok) { return typeof tok === 'string' && /^[A-Z][A-Za-z0-9_]*$/.test(tok); }

Try / catch

try {
  const data = await runtime.callFunction(fnName, args);
} catch (e) {
  if (String(e).includes('expected type parameter to be a Baml type')) {
    // replace args[1] with the type identifier (e.g. MyType) and retry
  } else throw e;
}

Prevention

When it happens

Trigger: Passing an instance/value (string, class instance, map) instead of a type reference as the second argument, e.g. `baml.fetch_as(url, my_instance)` instead of `baml.fetch_as(url, MyType)`.

Common situations: Confusing a type with a value, passing a stringly-named type instead of the type identifier, or generated code emitting a value where a type parameter is required.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at engine/baml-runtime/src/async_vm_runtime.rs:568

                                BamlValue::Class(name, _) if name == "baml.HttpRequest" => {} // Ok,

                                _ => {
                                    break 'mainloop Err(anyhow!(
                                        "baml.fetch_as: expected baml.fetch_as arg to be a string or HttpRequest, got {}",
                                        url_or_request
                                    ));
                                }
                            }

                            let parse_as_type = match vm
                                .objects
                                .as_object(&pending_future.args[1], baml_vm::ObjectType::Any)
                            {
                                Ok(idx) => match &vm.objects[idx] {
                                    baml_vm::Object::BamlType(type_ir) => type_ir.to_owned(),
                                    _ => {
                                        break 'mainloop Err(anyhow!(
                                            "baml.fetch_as: expected type parameter to be a Baml type, got {}",
                                            vm.objects[idx]
                                        ));
                                    }
                                },
                                Err(e) => {
                                    break 'mainloop Err(anyhow::Error::from(e).context(
                                        "baml.fetch_as: failed to get type parameter from VM value",
                                    ));
                                }
                            };

                            let future = {
                                let futures_tx = futures_tx.clone();
                                let cancel_tripwire = cancel_tripwire.to_owned();
                                let current_call_id = current_call_id.to_owned();

                                let output_format = jsonish::helpers::render_output_format(

View on GitHub (pinned to bd85ce9dee)