BoundaryML/baml · error · ConvertError

Failed to parse float

Error message

Failed to parse float: {0}

What it means

`ConvertError::ParseFloat` wraps `std::num::ParseFloatError` and is thrown by the sap_model converter when a value that should be a float literal cannot be parsed into `f64`. It occurs during conversion of parsed SAP/BLP structures into the typed model when a numeric literal string is malformed.

Solutions

  1. Inspect the wrapped ParseFloatError message to find the offending literal text, then fix the source literal.
  2. Ensure the lexer strips non-numeric decoration (suffixes, separators) before conversion.
  3. Normalize decimal separators (`,` -> `.`) in the token text before parsing.
  4. Update bex_sap if this is a regression from a recent lexer/parser change.

Example fix

// before
let f: f64 = tok.text.parse()?;
// after
let normalized = tok.text.replace('_', "");
let f: f64 = normalized.parse().map_err(|e| ConvertError::ParseFloat(e))?;
Defensive patterns

Strategy: try-catch

Validate before calling

fn is_valid_float_literal(s: &str) -> bool { s.replace('_', "").parse::<f64>().is_ok() }
// check literal text before conversion

Try / catch

match convert(tree) {
    Ok(m) => m,
    Err(ConvertError::ParseFloat(e)) => { eprintln!("bad float literal: {e}"); relex_and_retry()? }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: Converting a source tree (via the convert module) that contains a float literal token which `str::parse::<f64>()` rejects — e.g. an empty, malformed, or out-of-range numeric literal produced by an earlier lexing stage.

Common situations: A lexer change altered how numeric literals are tokenized (e.g. keeping suffixes or separators); source text written in a locale-dependent format (comma decimal separator); corrupted or hand-edited generated files.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at baml_language/crates/bex_sap/src/sap_model/convert.rs:24

use ::std::{
    collections::{HashMap, HashSet},
    sync::Arc,
};
use ::sys_types::{ClassDefinition, DefKey, EnumDefinition, SapTy};
use indexmap::IndexMap;

use crate::sap_model::{
    self, AnnotatedEnumVariant, AnnotatedField, AnnotatedTy, ArrayTy, AttrLiteral, BigintLiteralTy,
    BigintTy, BoolLiteralTy, BoolTy, ClassTy, EnumTy, EnumVariantTy, FloatTy, IntLiteralTy, IntTy,
    MapTy, MediaTy, NullTy, StringLiteralTy, StringTy, TyResolved, TyWithMeta, TypeAnnotations,
    TypeRefDb, UnionTy,
};

impl crate::sap_model::TypeIdent for DefKey {}

#[derive(thiserror::Error, Debug)]
pub enum ConvertError {
    #[error("Failed to parse float: {0}")]
    ParseFloat(#[from] std::num::ParseFloatError),
    #[error("Unknown media kind")]
    UnknownMediaKind,
    #[error("Float literals cannot be parsed")]
    FloatLiteral,
    #[error("Non-parsable type: {0:?}")]
    NonParsableType(Box<SapTy>),
    #[error("Unknown class: {0}")]
    UnknownClass(DefKey),
    #[error("Unknown enum: {0}")]
    UnknownEnum(DefKey),
    #[error("Unknown type alias: {0}")]
    UnknownTypeAlias(DefKey),
    #[error("Unknown name (could not determine if it was a class, enum, or type alias): {0}")]
    UnknownName(DefKey),
    #[error("Could not add a type to the database as the name `{0}` is already present")]
    AlreadyPresent(DefKey),
    #[error("Recursion depth exceeded for {0}")]

View on GitHub (pinned to bd85ce9dee)