BoundaryML/baml · error

encoding method arguments: %w

Error message

encoding method arguments: %w

What it means

This error wraps serde.EncodeMapEntries failures when encoding the kwargs map of an object method call into cffi HostMapEntry protobuf values. If any argument value cannot be serialized into the FFI wire format (unsupported type, nested structure that fails encoding), CallMethod returns this error before contacting the native side.

Source

Thrown at engine/language_client_go/baml_go/raw_objects/utils.go:159

}

func destructor(object RawPointer) error {
	result, err := CallMethod(object, "~destructor", nil)

	if err != nil {
		return fmt.Errorf("failed to call destructor: %w", err)
	}

	if result != nil {
		return fmt.Errorf("destructor returned unexpected result: %v", result)
	}
	return nil
}

func CallMethod(object RawPointer, method_name string, kwargs map[string]any) (any, error) {
	cffi_kwargs, err := serde.EncodeMapEntries(kwargs, "function arguments")
	if err != nil {
		return nil, fmt.Errorf("encoding method arguments: %w", err)
	}

	args := cffi.BamlObjectMethodInvocation{
		Kwargs:     cffi_kwargs,
		Object:     EncodeRawObject(object),
		MethodName: method_name,
	}

	encodedArgs, err := proto.Marshal(&args)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal object method arguments: %w", err)
	}
	cEncodedArgs := (*C.char)(unsafe.Pointer(&encodedArgs[0]))

	cBuf := C.WrapCallObjectMethodFunction(object.Runtime(), 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

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Read the wrapped %w error to find which argument key/value failed to encode
  2. Check the method signature and pass only supported types (string, int, bool, slices, maps of supported types)
  3. For type-builder methods pass the builder-handle objects (TypeBuilder/ClassBuilder returns), not raw strings or structs
  4. Convert custom structs into the supported primitive/builder types before calling

Example fix

// before
tb.Property("my_field") // kwargs with unsupported struct value
// after: pass supported types / builder handles
field := classBuilder.Property("my_field")
field.SetType(b.NewTypeBuilder(rt).Type("string"))
Defensive patterns

Strategy: validation

Validate before calling

// only pass FFI-encodable kwargs: string, int, float, bool, nil, slices/maps of these, or BAML builder objects
for k, v := range kwargs {
    switch v.(type) {
    case nil, string, int, int64, float64, bool, []any, map[string]any, b.TypeBuilder, b.ClassBuilder:
    default:
        return fmt.Errorf("kwarg %q has unsupported type %T", k, v)
    }
}

Try / catch

res, err := builder.AddProperty("name")
if err != nil {
    if strings.Contains(err.Error(), "encoding method arguments") {
        return fmt.Errorf("unsupported argument type: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling object methods such as TypeBuilder.AddProperty, ClassPropertyBuilder.SetType, collector methods, or b.Type(...) with kwargs containing values that serde cannot encode (e.g. unsupported Go types, channels, funcs, or invalid nested values).

Common situations: Passing wrong Go types to builder methods (e.g. a string where an enum/type ref is expected), passing nil maps or unsupported composite types, wrapping arbitrary structs not supported by the FFI serde layer.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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