{"record":{"id":"ca598950a028ecd7","repo":"BoundaryML/baml","slug":"baml-fetch-as-http-request-failed-http-status-body-body","errorCode":null,"errorMessage":"baml.fetch_as: HTTP request failed: HTTP {status}\nBody: {body}","messagePattern":"baml\\.fetch_as: HTTP request failed: HTTP (.+?)\nBody: (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/async_vm_runtime.rs","lineNumber":700,"sourceCode":"                                                break 'res Err(anyhow::Error::from(e).context(\n                                                    \"baml.fetch_as: failed to send request\",\n                                                ));\n                                            }\n                                        };\n\n                                        let status = res.status();\n\n                                        let body = match res.text().await {\n                                            Ok(body) => body,\n                                            Err(e) => {\n                                                break 'res Err(anyhow::Error::from(e).context(\n                                                    \"baml.fetch_as: failed to read response body\",\n                                                ));\n                                            }\n                                        };\n\n                                        if status.is_client_error() || status.is_server_error() {\n                                            break 'res Err(anyhow::anyhow!(\n                                            \"baml.fetch_as: HTTP request failed: HTTP {status}\\nBody: {body}\"\n                                        ));\n                                        }\n\n                                        jsonish::from_str(\n                                            &output_format,\n                                            &parse_as_type,\n                                            &body,\n                                            true,\n                                        )\n                                        .context(\n                                            \"(jsonish) Failed parsing response of fetch_value call\",\n                                        )\n                                    };\n\n                                    let response_baml_value = response.map(|r| {\n                                        ResponseBamlValue(\n                                            BamlValueWithMeta::<Vec<Flag>>::from(r).map_meta(","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/async_vm_runtime.rs#L682-L718","documentation":"After baml.fetch_as sends its HTTP request, the runtime checks the response status. If it is a 4xx client error or 5xx server error, it fails with this error including the status code and the response body, rather than trying to parse the body into the target type.","triggerScenarios":"baml.fetch_as(url, Type) where the remote endpoint returns HTTP 4xx/5xx — e.g. wrong API path, missing/invalid auth headers, bad request body, or the server being down.","commonSituations":"Calling a REST API with an expired API key (401), a mistyped endpoint (404), invalid JSON payload (400), or rate limiting (429); server-side crashes returning 500.","solutions":["Read the status and body in the error to identify the cause (auth, not-found, validation, server error)","Fix auth: supply correct API key/token in headers via the HttpRequest helper","Verify the URL path, query parameters and request body match the API contract","Handle retries for transient 5xx/429 with backoff"],"exampleFix":"// before (BAML)\nlet req = http.request(\"GET\", \"https://api.example.com/v1/items\")\nlet res = baml.fetch_as(req, Item[])\n// after (BAML)\nlet req = http.request(\"GET\", \"https://api.example.com/v1/items\")\n  .header(\"Authorization\", \"Bearer ${env.API_KEY}\")\nlet res = baml.fetch_as(req, Item[])","handlingStrategy":"retry","validationCode":"// pre-check endpoint health in a wrapper\n// if !endpoint_reachable(url) { skip fetch }","typeGuard":null,"tryCatchPattern":"// catch and inspect status before deciding to retry\nif err.contains(\"HTTP 4\") { don't retry } else if err.contains(\"HTTP 5\") || err.contains(\"429\") { retry with backoff }","preventionTips":["Set correct auth headers on every request","Validate URL path and body against the API docs","Add exponential backoff for 5xx/429","Monitor the upstream service status"],"tags":["http","network","fetch"],"backgroundTag":"http-error-response","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}