BoundaryML/baml · error
baml.fetch_as expects a string URL argument at {:?}
Error message
baml.fetch_as expects a string URL argument at {:?} What it means
After checking arity, baml.fetch_as validates that its single argument is a string URL. The interpreter throws this when the first argument evaluates to any non-string BamlValue (int, object, list, etc.).
Source
Thrown at engine/baml-compiler/src/thir/interpret.rs:2752
"baml.fetch_as" => {
if args.len() != 1 {
bail!(
"baml.fetch_as expects 1 argument (url), got {} at {:?}",
args.len(),
meta.0
);
}
if type_args.len() != 1 {
bail!(
"baml.fetch_as expects 1 type argument, got {} at {:?}",
type_args.len(),
meta.0
);
}
let url = match &args[0] {
BamlValueWithMeta::String(s, _) => s.clone(),
_ => bail!(
"baml.fetch_as expects a string URL argument at {:?}",
meta.0
),
};
let target_type = &type_args[0];
// Make HTTP request
let response = reqwest::get(&url).await.with_context(|| {
format!(
"baml.fetch_as: failed to fetch URL '{}' at {:?}",
url, meta.0
)
})?;
let status = response.status();
if !status.is_success() {
let body = responseView on GitHub (pinned to bd85ce9dee)
Solutions
- Ensure the first argument is a string literal or an expression evaluating to a string
- Coerce or extract the URL, e.g. pass obj.url instead of obj
- Inspect the value at the reported span and add an explicit string construction
Example fix
// before
baml.fetch_as::<MyType>(config.endpoint_port)
// after
baml.fetch_as::<MyType>("https://example.com/endpoint") Defensive patterns
Strategy: type-guard
Validate before calling
fn ensure_string_url(args: &[BamlValueWithMeta<ExprMetadata>]) -> Result<String, String> {
match args.first() {
Some(BamlValueWithMeta::String(s, _)) => Ok(s.clone()),
_ => Err("baml.fetch_as first argument must be a string URL".into()),
}
} Type guard
fn is_string(v: &BamlValueWithMeta<ExprMetadata>) -> bool {
matches!(v, BamlValueWithMeta::String(_, _))
} Prevention
- Keep URLs in explicitly typed string variables
- Interpolate dynamic parts into a string literal instead of passing raw values
- Add pre-evaluation checks that the URL argument is a string
When it happens
Trigger: Calling baml.fetch_as::<T>(x) where x evaluates to a non-string value, e.g. an int, a field of a map, or the result of a non-string expression.
Common situations: Passing a URL stored in a non-string variable; forgetting to interpolate into a string; passing a parsed JSON object instead of the URL string field within it.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- toLowerCase() method only available on strings at {:?}
- type `{ty}` can't be passed through auto-CLI; deliver it via
- baml.json.deserialize failed: {e:?}
- baml.json.serialize returned non-string value: {other:?}
- Type mismatch: {message}
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/1b5ffe57fed729f9.
Report an issue: GitHub.