BoundaryML/baml · error

baml.fetch_as: expected baml.fetch_as arg to be a string or…

Error message

baml.fetch_as: expected baml.fetch_as arg to be a string or HttpRequest, got {}

What it means

The first argument of `baml.fetch_as` must be either a plain string URL or a `baml.HttpRequest` class instance. Any other BamlValue (int, map, list, other class) is rejected before building the reqwest request.

Solutions

  1. Pass a plain string URL: baml.fetch_as("https://...", MyType)
  2. Or construct a proper baml.HttpRequest class with url/method/headers fields and pass that
  3. Coerce/validate the first argument is a string before the call

Example fix

// before (BAML)
let opts = {url: "https://api.example.com", method: "Get"}
let data = baml.fetch_as(opts, MyType)
// after
let req = baml.HttpRequest{url: "https://api.example.com", method: "Get"}
let data = baml.fetch_as(req, MyType)
Defensive patterns

Strategy: type-guard

Validate before calling

function isValidFetchAsFirstArg(arg) {
  return typeof arg === 'string' || (arg && arg.type === 'baml.HttpRequest');
}

Type guard

function isStringOrHttpRequest(v) {
  return typeof v === 'string' ||
    (v !== null && typeof v === 'object' && v.constructor?.name === 'HttpRequest' && typeof v.url === 'string');
}

Try / catch

try {
  const data = await runtime.callFunction(fnName, args);
} catch (e) {
  if (String(e).includes('expected baml.fetch_as arg to be a string or HttpRequest')) {
    // log args[0] and fix it to a string URL or baml.HttpRequest instance
  } else throw e;
}

Prevention

When it happens

Trigger: Passing a Map/object literal, an int, or a different class as the first argument to `baml.fetch_as`; also occurs when a dynamic value resolves to a non-string non-HttpRequest type at runtime.

Common situations: Passing a URL from an untyped variable that holds something else, constructing a hand-rolled request object that is not exactly named `baml.HttpRequest`, or JSON-deserialized data fed directly as the URL.

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/e47c73e9b9ee0cbd. Report an issue: GitHub.

Appendix: source

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

                            }

                            let url_or_request =
                                match try_baml_value_from_vm_value(&vm, &pending_future.args[0]) {
                                    Ok(url_or_request) => url_or_request,
                                    Err(e) => {
                                        break 'mainloop Err(e.context(
                                        "baml.fetch_as: failed to get url or request from VM value",
                                    ));
                                    }
                                };

                            match &url_or_request {
                                BamlValue::String(_) => {} // Ok,

                                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]
                                        ));
                                    }

View on GitHub (pinned to bd85ce9dee)