BoundaryML/baml · error

unexpected type for literal string type: %T

Error message

unexpected type for literal string type: %T

What it means

TypeBuilder.LiteralString() builds a literal-string FieldType and asserts the FFI callback result implements the Type interface. A non-Type result (usually nil from a failed native call) triggers this internal-invariant error. It indicates a broken/bridged type-builder rather than invalid user input.

Solutions

  1. Pin engine/language_client_go and the native baml library to matching versions and run `baml-cli generate` again.
  2. Verify the literal value passed is a non-empty, valid string the native layer accepts.
  3. Confirm the TypeBuilder was obtained from a live runtime/baml client, not constructed manually.
  4. Reproduce with the %T value logged and report to github.com/boundaryml/baml if versions match.

Example fix

// before
lit, err := tb.LiteralString("") // native call fails, result nil

// after
if val == "" {
    return nil, errors.New("literal string must be non-empty")
}
lit, err := tb.LiteralString(val)
Defensive patterns

Strategy: try-catch

Validate before calling

if val == "" {
    return nil, errors.New("literal string value must be non-empty")
}

Type guard

func isNonEmptyString(s string) bool { return s != "" }

Try / catch

lit, err := tb.LiteralString(val)
if err != nil {
    return nil, fmt.Errorf("LiteralString(%q) failed: %w", val, err)
}

Prevention

When it happens

Trigger: Calling TypeBuilder.LiteralString("value") when the underlying native type-builder call fails or returns a value that fails the `result.(Type)` assertion, e.g. nil from an incompatible FFI.

Common situations: Version skew between the Go client and the bundled baml engine library; passing an empty string to a native layer that rejects it and returns nil; calling after the builder was finalized.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at engine/language_client_go/pkg/rawobjects_type_builder.go:107

		return nil, fmt.Errorf("unexpected type for null type: %T", result)
	}

	return typ, nil
}

// Literal types
func (tb *typeBuilder) LiteralString(value string) (Type, error) {
	args := map[string]interface{}{
		"value": value,
	}
	result, err := raw_objects.CallMethod(tb, "literal_string", args)
	if err != nil {
		return nil, err
	}

	typ, ok := result.(Type)
	if !ok {
		return nil, fmt.Errorf("unexpected type for literal string type: %T", result)
	}

	return typ, nil
}

func (tb *typeBuilder) LiteralInt(value int64) (Type, error) {
	args := map[string]interface{}{
		"value": value,
	}
	result, err := raw_objects.CallMethod(tb, "literal_int", args)
	if err != nil {
		return nil, err
	}

	typ, ok := result.(Type)
	if !ok {
		return nil, fmt.Errorf("unexpected type for literal int type: %T", result)
	}

View on GitHub (pinned to bd85ce9dee)