{"record":{"id":"e47c73e9b9ee0cbd","repo":"BoundaryML/baml","slug":"baml-fetch-as-expected-baml-fetch-as-arg-to-be-a-string-or","errorCode":null,"errorMessage":"baml.fetch_as: expected baml.fetch_as arg to be a string or HttpRequest, got {}","messagePattern":"baml\\.fetch_as: expected baml\\.fetch_as arg to be a string or HttpRequest, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/async_vm_runtime.rs","lineNumber":554,"sourceCode":"                            }\n\n                            let url_or_request =\n                                match try_baml_value_from_vm_value(&vm, &pending_future.args[0]) {\n                                    Ok(url_or_request) => url_or_request,\n                                    Err(e) => {\n                                        break 'mainloop Err(e.context(\n                                        \"baml.fetch_as: failed to get url or request from VM value\",\n                                    ));\n                                    }\n                                };\n\n                            match &url_or_request {\n                                BamlValue::String(_) => {} // Ok,\n\n                                BamlValue::Class(name, _) if name == \"baml.HttpRequest\" => {} // Ok,\n\n                                _ => {\n                                    break 'mainloop Err(anyhow!(\n                                        \"baml.fetch_as: expected baml.fetch_as arg to be a string or HttpRequest, got {}\",\n                                        url_or_request\n                                    ));\n                                }\n                            }\n\n                            let parse_as_type = match vm\n                                .objects\n                                .as_object(&pending_future.args[1], baml_vm::ObjectType::Any)\n                            {\n                                Ok(idx) => match &vm.objects[idx] {\n                                    baml_vm::Object::BamlType(type_ir) => type_ir.to_owned(),\n                                    _ => {\n                                        break 'mainloop Err(anyhow!(\n                                            \"baml.fetch_as: expected type parameter to be a Baml type, got {}\",\n                                            vm.objects[idx]\n                                        ));\n                                    }","sourceCodeStart":536,"sourceCodeEnd":572,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/async_vm_runtime.rs#L536-L572","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a plain string URL: baml.fetch_as(\"https://...\", MyType)","Or construct a proper baml.HttpRequest class with url/method/headers fields and pass that","Coerce/validate the first argument is a string before the call"],"exampleFix":"// before (BAML)\nlet opts = {url: \"https://api.example.com\", method: \"Get\"}\nlet data = baml.fetch_as(opts, MyType)\n// after\nlet req = baml.HttpRequest{url: \"https://api.example.com\", method: \"Get\"}\nlet data = baml.fetch_as(req, MyType)","handlingStrategy":"type-guard","validationCode":"function isValidFetchAsFirstArg(arg) {\n  return typeof arg === 'string' || (arg && arg.type === 'baml.HttpRequest');\n}","typeGuard":"function isStringOrHttpRequest(v) {\n  return typeof v === 'string' ||\n    (v !== null && typeof v === 'object' && v.constructor?.name === 'HttpRequest' && typeof v.url === 'string');\n}","tryCatchPattern":"try {\n  const data = await runtime.callFunction(fnName, args);\n} catch (e) {\n  if (String(e).includes('expected baml.fetch_as arg to be a string or HttpRequest')) {\n    // log args[0] and fix it to a string URL or baml.HttpRequest instance\n  } else throw e;\n}","preventionTips":["Type-check the first argument before the call","Never pass raw object literals; wrap them in baml.HttpRequest"],"tags":["baml","type-mismatch","fetch","arguments"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}