{"record":{"id":"fd8305db3da83004","repo":"BoundaryML/baml","slug":"failed-to-marshal-object-constructor-arguments-w","errorCode":null,"errorMessage":"failed to marshal object constructor arguments: %w","messagePattern":"failed to marshal object constructor arguments: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/raw_objects/utils.go","lineNumber":114,"sourceCode":"\nfunc (r *RawObject) Runtime() unsafe.Pointer {\n\treturn r.baml_runtime\n}\n\nfunc FromPointer(ptr int64, rt unsafe.Pointer) *RawObject {\n\treturn &RawObject{ptr: ptr, baml_runtime: rt}\n}\n\n// newRawObject creates a new refcounted rawObject\nfunc NewRawObject(rt unsafe.Pointer, objectType cffi.BamlObjectType, kwargs []*cffi.HostMapEntry) (any, error) {\n\targs := cffi.BamlObjectConstructorInvocation{\n\t\tType:   objectType,\n\t\tKwargs: kwargs,\n\t}\n\n\tencodedArgs, err := proto.Marshal(&args)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal object constructor arguments: %w\", err)\n\t}\n\tcEncodedArgs := (*C.char)(unsafe.Pointer(&encodedArgs[0]))\n\n\tcBuf := C.WrapCallObjectConstructor(cEncodedArgs, C.uintptr_t(len(encodedArgs)))\n\n\tcontent_bytes := C.GoBytes(unsafe.Pointer(cBuf.ptr), C.int32_t(cBuf.len))\n\tC.WrapFreeBuffer(cBuf) // Free the buffer after use\n\n\tif cBuf.len == 0 {\n\t\treturn nil, fmt.Errorf(\"failed to call object constructor\")\n\t}\n\tif cBuf.ptr == nil {\n\t\treturn nil, fmt.Errorf(\"object constructor returned nil pointer\")\n\t}\n\n\tvar content_holder cffi.InvocationResponse\n\terr = proto.Unmarshal(content_bytes, &content_holder)\n\tif err != nil {","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/raw_objects/utils.go#L96-L132","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nobj, err := NewRawObject(ctx, \"Image\", map[string]any{\n    \"url\": complexValue, // unsupported type\n})\n\n// after\nobj, err := NewRawObject(ctx, \"Image\", map[string]any{\n    \"url\": \"https://example.com/img.png\", // supported primitive\n})\nif err != nil {\n    return fmt.Errorf(\"raw object: %w\", err)\n}","handlingStrategy":"validation","validationCode":"for k, v := range kwargs {\n    switch v.(type) {\n    case string, int, int64, float64, bool, []byte:\n    default:\n        return fmt.Errorf(\"unsupported kwarg type for %s: %T\", k, v)\n    }\n}","typeGuard":"func protoEncodable(v any) bool {\n    switch v.(type) {\n    case string, int, int64, float64, bool, []byte:\n        return true\n    default:\n        return false\n    }\n}","tryCatchPattern":"obj, err := NewRawObject(ctx, objectType, kwargs)\nif err != nil {\n    var me *marshalError\n    if errors.As(err, &me) {\n        return fmt.Errorf(\"fix kwarg types for %s: %w\", objectType, err)\n    }\n    return err\n}","preventionTips":["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."],"tags":["protobuf","serialization","cffi","arguments"],"backgroundTag":"json-marshal-failed","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}