BoundaryML/baml · error

baml.fetch_as: expected method to be a valid HTTP method, go

Error message

baml.fetch_as: expected method to be a valid HTTP method, got {}

What it means

When reading a baml.HttpRequest, the `method` field must be an Enum value holding a valid HTTP method name. The field was missing or not an enum, so the runtime cannot determine the verb to use. (This branch fires when the field is absent or the wrong type; the message prints the whole request object.)

Source

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

                                    let response = 'res: {
                                        let client = reqwest::Client::new();

                                        let req = match &url_or_request {
                                            BamlValue::String(url) => {
                                                client.get(url)
                                            }

                                            // we type checked earlier, should be http request type
                                            BamlValue::Class(name, fields) => {
                                                let Some(BamlValue::String(url)) = fields.get("url") else {
                                                    break 'res Err(anyhow!(
                                                        "baml.fetch_as: expected url to be a string, got {}",
                                                        url_or_request
                                                    ));
                                                };

                                                let Some(BamlValue::Enum(_, method)) = fields.get("method") else {
                                                    break 'res Err(anyhow!(
                                                        "baml.fetch_as: expected method to be a valid HTTP method, got {}",
                                                        url_or_request
                                                    ));
                                                };

                                                let mut req = match method.as_str() {
                                                    "Get" => client.get(url),
                                                    "Post" => client.post(url),
                                                    "Put" => client.put(url),
                                                    "Patch" => client.patch(url),
                                                    "Delete" => client.delete(url),
                                                    _ => break 'res Err(anyhow!(
                                                        "baml.fetch_as: expected method to be a valid HTTP method, got {}",
                                                        method
                                                    ))
                                                };

                                                if let Some(BamlValue::Map(headers)) = fields.get("headers") {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Set `method` on the baml.HttpRequest (e.g. method: "Get") — there is no default
  2. Ensure method is supplied as the enum form the runtime expects, not a free-form string
  3. Use one of the supported spellings: Get, Post, Put, Patch, Delete

Example fix

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

Strategy: validation

Validate before calling

function validateMethod(req) {
  if (req.method === undefined || req.method === null) {
    throw new Error('baml.HttpRequest.method is required (Get|Post|Put|Patch|Delete)');
  }
}

Type guard

function hasMethod(req) { return typeof req === 'object' && req !== null && req.method != null; }

Try / catch

try {
  const data = await runtime.callFunction(fnName, args);
} catch (e) {
  if (String(e).includes('expected method to be a valid HTTP method')) {
    // add/fix the method field on the baml.HttpRequest and retry
  } else throw e;
}

Prevention

When it happens

Trigger: Constructing baml.HttpRequest without a `method` field, or with a plain string/other value instead of an enum value for method.

Common situations: Passing method as a raw string like "GET" instead of the enum value, omitting method assuming a default (none exists), or building the request from JSON where method became a string.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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