BoundaryML/baml · error

expected 2 arguments for `baml.fetch_as`, got

Error message

expected 2 arguments for `baml.fetch_as`, got {}

What it means

`baml.fetch_as` takes exactly two arguments — the URL/HttpRequest value and the BAML type to parse the response as. The VM found a different argument count and aborts the main loop before executing the request.

Solutions

  1. Pass exactly two arguments: the URL (or baml.HttpRequest) and the BAML type to parse as
  2. If you need headers/body/method, build a baml.HttpRequest class and pass it as the single first argument instead of extra positional args
  3. Count arguments in the .baml call site; fix any missing or extra ones

Example fix

// before (BAML)
let data = baml.fetch_as("https://api.example.com/data", MyType, {headers: {"x-a": "1"}})
// after
let req = baml.HttpRequest{url: "https://api.example.com/data", headers: {"x-a": "1"}}
let data = baml.fetch_as(req, MyType)
Defensive patterns

Strategy: validation

Validate before calling

// ensure baml.fetch_as call sites have exactly 2 arguments
function validateFetchAsArity(bamlSrc) {
  const re = /baml\.fetch_as\s*\(([^()]*)\)/g;
  for (const m of bamlSrc.matchAll(re)) {
    const argc = m[1].split(',').filter(a => a.trim() !== '').length;
    if (argc !== 2) throw new Error(`baml.fetch_as needs exactly 2 args, found ${argc}: ${m[0]}`);
  }
}

Try / catch

try {
  const result = await runtime.callFunction(fnName, args);
} catch (e) {
  if (/expected 2 arguments for `baml.fetch_as`/.test(String(e))) {
    // fix call site arity in .baml source and retry
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `baml.fetch_as` with fewer than 2 args (missing the target type or the URL) or more than 2 (extra params such as headers passed positionally instead of in an HttpRequest).

Common situations: Forgetting the second type argument (`baml.fetch_as(url)`), passing options as extra positional args, or a code generator / transpiler emitting a wrong call arity.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

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

                            // basically turns Rust futures into JavaScript promises,
                            // which are supposed to be single threaded. So, on WASM,
                            // except for compilation required types, spawn and
                            // spawn_local are essentially equivalent.
                            #[cfg(target_arch = "wasm32")]
                            wasm_bindgen_futures::spawn_local(future);
                        }

                        baml_vm::FutureKind::Net => {
                            // Only `baml.fetch_as` is supported for now.
                            if pending_future.function != "baml.fetch_as" {
                                break 'mainloop Err(anyhow!(
                                    "unkown function: {}",
                                    pending_future.function
                                ));
                            }

                            if pending_future.args.len() != 2 {
                                break 'mainloop Err(anyhow!(
                                    "expected 2 arguments for `baml.fetch_as`, got {}",
                                    pending_future.args.len()
                                ));
                            }

                            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,

View on GitHub (pinned to bd85ce9dee)