BoundaryML/baml · error
failed to marshal object constructor arguments: %w
Error message
failed to marshal object constructor arguments: %w
What it means
NewRawObject marshals the object type and its kwargs into a protobuf (proto.Marshal) before sending them over the CFFI boundary to the BAML native runtime. This error means that marshaling failed, so the constructor arguments could not be serialized for the native call; the original proto error is wrapped. In practice this indicates kwargs contain values the protobuf schema cannot encode.
Source
Thrown at engine/language_client_go/baml_go/raw_objects/utils.go:114
func (r *RawObject) Runtime() unsafe.Pointer {
return r.baml_runtime
}
func FromPointer(ptr int64, rt unsafe.Pointer) *RawObject {
return &RawObject{ptr: ptr, baml_runtime: rt}
}
// newRawObject creates a new refcounted rawObject
func NewRawObject(rt unsafe.Pointer, objectType cffi.BamlObjectType, kwargs []*cffi.HostMapEntry) (any, error) {
args := cffi.BamlObjectConstructorInvocation{
Type: objectType,
Kwargs: kwargs,
}
encodedArgs, err := proto.Marshal(&args)
if err != nil {
return nil, fmt.Errorf("failed to marshal object constructor arguments: %w", err)
}
cEncodedArgs := (*C.char)(unsafe.Pointer(&encodedArgs[0]))
cBuf := C.WrapCallObjectConstructor(cEncodedArgs, C.uintptr_t(len(encodedArgs)))
content_bytes := C.GoBytes(unsafe.Pointer(cBuf.ptr), C.int32_t(cBuf.len))
C.WrapFreeBuffer(cBuf) // Free the buffer after use
if cBuf.len == 0 {
return nil, fmt.Errorf("failed to call object constructor")
}
if cBuf.ptr == nil {
return nil, fmt.Errorf("object constructor returned nil pointer")
}
var content_holder cffi.InvocationResponse
err = proto.Unmarshal(content_bytes, &content_holder)
if err != nil {View on GitHub (pinned to bd85ce9dee)
Solutions
- Inspect the wrapped %w error to identify which field/value failed to marshal and fix the offending kwarg value type.
- Ensure kwargs values are supported primitives/strings/bytes consistent with the BAML object type's expected schema.
- For large media, check size limits and truncate/stream if the payload exceeds proto's practical limits (default 2GB, but memory-bound well before).
- Rebuild with matching versions of the baml Go module and generated proto code; report a bug if valid arguments still fail.
Example fix
// before
obj, err := NewRawObject(ctx, "Image", map[string]any{
"url": complexValue, // unsupported type
})
// after
obj, err := NewRawObject(ctx, "Image", map[string]any{
"url": "https://example.com/img.png", // supported primitive
})
if err != nil {
return fmt.Errorf("raw object: %w", err)
} Defensive patterns
Strategy: validation
Validate before calling
for k, v := range kwargs {
switch v.(type) {
case string, int, int64, float64, bool, []byte:
default:
return fmt.Errorf("unsupported kwarg type for %s: %T", k, v)
}
} Type guard
func protoEncodable(v any) bool {
switch v.(type) {
case string, int, int64, float64, bool, []byte:
return true
default:
return false
}
} Try / catch
obj, err := NewRawObject(ctx, objectType, kwargs)
if err != nil {
var me *marshalError
if errors.As(err, &me) {
return fmt.Errorf("fix kwarg types for %s: %w", objectType, err)
}
return err
} Prevention
- Pass only schema-supported primitive types in kwargs.
- Keep media payloads within practical size limits.
- Keep generated proto code and the baml module on matching versions.
When it happens
Trigger: Calling NewRawObject — directly or via NewCollector, newMediaFromUrl, newMediaFromBase64, or NewTypeBuilder — with kwargs containing types not representable in the expected proto message (e.g. unsupported value types, NaN/Inf floats where disallowed, oversized payloads).
Common situations: Passing arbitrary Go maps/slices with unexpected value kinds into builder/collector APIs; media payloads exceeding proto limits; a bug or version mismatch between generated proto code and the runtime library.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- Invalid command arguments for command: {command}: {message}
- failed to call object constructor
- encoding method arguments: %w
- encoding client options: %w
- encoding function arguments: %w
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/fd8305db3da83004.
Report an issue: GitHub.