BoundaryML/baml · error

failed to get duration

Error message

failed to get duration: %w

What it means

timing.DurationMs() invokes the FFI method "duration_ms" on the underlying timing object; any bridge error is wrapped as "failed to get duration: %w". Note the method deliberately returns (nil, nil) when the result is nil (duration not yet known), so this error only fires on a genuine native-side failure such as an invalid object handle or missing method.

Solutions

  1. Unwrap the error chain to find the native cause before changing your code.
  2. Read duration within the lifetime of the FunctionLog/LLMCall and its runtime.
  3. Align the baml Go module version with your BAML native runtime.
  4. Treat nil duration (no error) as 'not available yet' - the API distinguishes this from failure; don't conflate the two.

Example fix

// before
dur, err := timing.DurationMs()
if err != nil || dur == nil {
    return errors.New("no duration")
}

// after: handle nil vs error distinctly
dur, err := timing.DurationMs()
if err != nil {
    return fmt.Errorf("duration read failed: %w", err)
}
if dur == nil {
    return nil // call still in flight; duration not finalized
}
Defensive patterns

Strategy: try-catch

Validate before calling

if timing == nil {
    return errors.New("timing object is nil")
}

Try / catch

dur, err := timing.DurationMs()
switch {
case err != nil:
    return fmt.Errorf("duration read failed: %w", err)
case dur == nil:
    return nil // not finalized yet
}

Prevention

When it happens

Trigger: Calling DurationMs() on a Timing (from function logs or LLM calls, e.g. in BAML collectors) while the underlying "duration_ms" FFI call fails - typically a stale/invalid raw object pointer or a runtime version mismatch.

Common situations: Reading duration after the runtime/context was released; a call that was interrupted so timing metadata was never finalized; Go bindings not matching the installed native BAML version.

Related errors


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

Appendix: source

Thrown at engine/language_client_go/pkg/rawobjects_timing.go:44

func (t *timing) StartTimeUTCMs() (int64, error) {
	result, err := raw_objects.CallMethod(t, "start_time_utc_ms", nil)
	if err != nil {
		return 0, fmt.Errorf("failed to get start time: %w", err)
	}

	startTime, ok := result.(int64)
	if !ok {
		return 0, fmt.Errorf("unexpected type for start time: %T", result)
	}

	return startTime, nil
}

func (t *timing) DurationMs() (*int64, error) {
	result, err := raw_objects.CallMethod(t, "duration_ms", nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get duration: %w", err)
	}

	if result == nil {
		return nil, nil
	}

	duration, ok := result.(int64)
	if !ok {
		return nil, fmt.Errorf("unexpected type for duration: %T", result)
	}

	return &duration, nil
}

View on GitHub (pinned to bd85ce9dee)