{"record":{"id":"8be287fd4f80040d","repo":"BoundaryML/baml","slug":"baml-fetch-as-expected-header-value-to-be-a-string-got","errorCode":null,"errorMessage":"baml.fetch_as: expected header value to be a string, got {}","messagePattern":"baml\\.fetch_as: expected header value to be a string, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/async_vm_runtime.rs","lineNumber":643,"sourceCode":"                                                    _ => break 'res Err(anyhow!(\n                                                        \"baml.fetch_as: expected method to be a valid HTTP method, got {}\",\n                                                        method\n                                                    ))\n                                                };\n\n                                                if let Some(BamlValue::Map(headers)) = fields.get(\"headers\") {\n                                                    let mut header_map = reqwest::header::HeaderMap::new();\n\n                                                    for (k, v) in headers {\n                                                        let Ok(key) = reqwest::header::HeaderName::from_str(k) else {\n                                                            break 'res Err(anyhow!(\n                                                                \"baml.fetch_as: expected header key to be a valid HTTP header name, got {}\",\n                                                                k\n                                                            ));\n                                                        };\n\n                                                        let Some(value_as_string) = v.as_str() else {\n                                                            break 'res Err(anyhow!(\n                                                                \"baml.fetch_as: expected header value to be a string, got {}\",\n                                                                v\n                                                            ));\n                                                        };\n\n                                                        let Ok(value) = reqwest::header::HeaderValue::from_str(value_as_string) else {\n                                                            break 'res Err(anyhow!(\n                                                                \"baml.fetch_as: expected header value to be a string, got {}\",\n                                                                v\n                                                            ));\n                                                        };\n\n                                                        header_map.insert(key, value);\n                                                    }\n\n                                                    req = req.headers(header_map);\n                                                }\n","sourceCodeStart":625,"sourceCodeEnd":661,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/async_vm_runtime.rs#L625-L661","documentation":"Runtime type-validation error inside the baml.fetch_as host function: the `headers` field of the request argument carried a value that is not a string (e.g. an int or nested map) when building the HTTP HeaderMap. The per-header type check fires during request construction, before any network I/O.","triggerScenarios":"Passing a header value that is not a string, e.g. headers{\"Content-Length\": 42} or a null value from an unresolved variable.","commonSituations":"Numeric or boolean header values (content-length, retries), headers built from JSON where numbers stay numbers, or template variables that resolve to null.","solutions":["Convert header values to strings before putting them in the headers map (e.g. 42 → \"42\")","Ensure variables used as header values are non-null strings at runtime","Restructure headers so only string values are included"],"exampleFix":"// before (BAML)\nlet req = baml.HttpRequest{url: \"https://api.example.com\", method: \"Get\", headers: {\"Content-Length\": 42}}\n// after\nlet req = baml.HttpRequest{url: \"https://api.example.com\", method: \"Get\", headers: {\"Content-Length\": \"42\"}}","handlingStrategy":"type-guard","validationCode":"function validateHeaderValues(headers) {\n  for (const [k, v] of Object.entries(headers || {})) {\n    if (typeof v !== 'string') throw new Error(`Header \"${k}\" must be a string, got ${typeof v}: ${JSON.stringify(v)}`);\n  }\n}","typeGuard":"function isStringHeaderMap(h) {\n  return h !== null && typeof h === 'object' && Object.values(h).every(v => typeof v === 'string');\n}","tryCatchPattern":"try {\n  const data = await runtime.callFunction(fnName, args);\n} catch (e) {\n  if (String(e).includes('expected header value to be a string')) {\n    // coerce numeric/boolean header values with String(v) and retry\n  } else throw e;\n}","preventionTips":["Stringify numeric/boolean header values (Content-Length, retries) before the call","Guard template variables used as header values against null/undefined","Keep headers typed as Record<string,string> end-to-end"],"tags":["baml","http","headers","type-mismatch"],"backgroundTag":"type-mismatch","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"}