BoundaryML/baml · error

unexpected type for text: %T

Error message

unexpected type for text: %T

What it means

sseResponse.Text() calls the underlying FFI object's "text" method via raw_objects.CallMethod and asserts the returned any is a Go string. When the cross-language bridge returns a value that is not a string, the assertion fails and this error is thrown. It indicates the raw object backing the SSEResponse did not yield text of the expected type, usually due to a version mismatch between the Go bindings and the native BAML runtime or an internal bridge bug.

Source

Thrown at engine/language_client_go/pkg/rawobjects_sse_response.go:35

}

func (s *sseResponse) ObjectType() cffi.BamlObjectType {
	return cffi.BamlObjectType_OBJECT_SSE_RESPONSE
}

func (s *sseResponse) pointer() int64 {
	return s.RawObject.Pointer()
}

func (s *sseResponse) Text() (string, error) {
	result, err := raw_objects.CallMethod(s, "text", nil)
	if err != nil {
		return "", fmt.Errorf("failed to get text: %w", err)
	}

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

	return text, nil
}

func (s *sseResponse) JSON() (any, error) {
	result, err := raw_objects.CallMethod(s, "json", nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get JSON: %w", err)
	}

	return result, nil
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check that the baml Go module version exactly matches your BAML CLI/native runtime version (go.mod vs `baml version`) and upgrade both together.
  2. Print the offending type with errors like `unexpected type for text: %T` to identify what the bridge actually returned, then file/inspect the raw_objects.CallMethod decoder for that type.
  3. If the SSE payload may be non-text, prefer JSON() over Text() and decode yourself.
  4. Update the baml dependency (`go get -u github.com/boundaryml/baml/...`) to pick up FFI type-decoding fixes.

Example fix

// before
text, err := sseResp.Text()
if err != nil {
    return err
}

// after: fall back to JSON when text decoding fails
text, err := sseResp.Text()
if err != nil {
    var anyResp any
    anyResp, err = sseResp.JSON()
    if err != nil {
        return err
    }
    _ = anyResp
}
Defensive patterns

Strategy: try-catch

Validate before calling

if sseResp == nil {
    return errors.New("sse response is nil")
}

Type guard

func isString(v any) bool { _, ok := v.(string); return ok }

Try / catch

text, err := sseResp.Text()
if err != nil {
    if strings.Contains(err.Error(), "unexpected type for text") {
        log.Printf("non-string SSE payload, falling back to JSON: %v", err)
        return sseResp.JSON()
    }
    return fmt.Errorf("sse text: %w", err)
}

Prevention

When it happens

Trigger: Calling Text() on an SSEResponse whose underlying "text" FFI method returns a non-string value (e.g. nil, []byte, or a wrapped object) instead of a Go string.

Common situations: Using a baml_go Go binding version that does not match the installed native BAML runtime; the FFI decoder returns a differently-typed payload (e.g. []byte instead of string); a streaming SSE event carries non-text payload that the bridge mis-decodes.

Related errors


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