BoundaryML/baml · error

shift <<= requires integer operands

Error message

shift <<= requires integer operands

What it means

Raised by the BAML interpreter when evaluating `x <<= y` where either operand is not an Int. Left-shift-assign is implemented only for the Int/Int pair; all other type combinations fall through to this bail.

Source

Thrown at engine/baml-compiler/src/thir/interpret.rs:997

                                        BamlValueWithMeta::Int(b, _),
                                    ) => BamlValueWithMeta::Int(a & b, meta),
                                    _ => bail!("bitwise &= requires integer operands"),
                                }
                            }
                            AssignOp::BitOrAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    BamlValueWithMeta::Int(a | b, meta)
                                }
                                _ => bail!("bitwise |= requires integer operands"),
                            },
                            AssignOp::ShlAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    if b < 0 {
                                        bail!("negative shift amount in <<= operator");
                                    }
                                    BamlValueWithMeta::Int(a << b, meta)
                                }
                                _ => bail!("shift <<= requires integer operands"),
                            },
                            AssignOp::ShrAssign => match (current_val.clone(), rhs_val.clone()) {
                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
                                    if b < 0 {
                                        bail!("negative shift amount in >>= operator");
                                    }
                                    BamlValueWithMeta::Int(a >> b, meta)
                                }
                                _ => bail!("shift >>= requires integer operands"),
                            },
                        };

                        // Assign the result back to the target expression
                        assign_to_expr(
                            left,
                            result_val,
                            scopes,
                            thir,

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Ensure both operands are Int; round/convert float shift amounts with an explicit integer conversion.
  2. Use integer literals without decimal points for constant shifts.
  3. Verify the shifted variable itself holds an Int.

Example fix

// before (BAML)
x <<= steps / 2; // error if result is float: shift <<= requires integer operands

// after
x <<= toInt(steps / 2); // ensure Int operands
Defensive patterns

Strategy: type-guard

Validate before calling

// verify both shift operands are ints before <<= executes
function assertShlOperands(value: unknown, amount: unknown): void {
  if (!Number.isInteger(value) || !Number.isInteger(amount)) {
    throw new Error("shift <<= requires integer operands in BAML");
  }
}

Type guard

const isInt = (v: unknown): v is number => typeof v === "number" && Number.isInteger(v);

Try / catch

try {
  await runBaml(program);
} catch (e) {
  if (String(e).includes("shift <<= requires integer operands")) {
    // convert operands to Int (round computed amounts) and retry
  } else { throw e; }
}

Prevention

When it happens

Trigger: Executing `x <<= y` where `x` or `y` is a Float, Bool, String, etc. — e.g. shifting by a float amount `x <<= 2.0` or shifting a non-int value.

Common situations: Shift amounts computed from float math (division results), or literals written as `2.0` out of habit; also shifting values parsed from LLM output as strings.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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