{"record":{"id":"e930f2f277191f7d","repo":"BoundaryML/baml","slug":"encoding-function-arguments-w","errorCode":null,"errorMessage":"encoding function arguments: %w","messagePattern":"encoding function arguments: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/pkg/rawobjects_function_args.go","lineNumber":32,"sourceCode":"\tClientRegistry *ClientRegistry\n\tEnv            map[string]string\n\tCollectors     []Collector\n\tTypeBuilder    TypeBuilder\n\tTags           map[string]string\n}\n\nfunc (args *BamlFunctionArguments) Encode() ([]byte, error) {\n\tencoded, err := args.encode()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn proto.Marshal(encoded)\n}\n\nfunc (args *BamlFunctionArguments) encode() (*cffi.HostFunctionArguments, error) {\n\tkwargs, err := serde.EncodeMapEntries(args.Kwargs, \"function arguments\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"encoding function arguments: %w\", err)\n\t}\n\n\tvar clientRegistry *cffi.HostClientRegistry\n\tif args.ClientRegistry != nil {\n\t\tclientRegistry, err = encodeClientRegistry(args.ClientRegistry)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding client registry: %w\", err)\n\t\t}\n\t}\n\n\tvar env []*cffi.HostEnvVar\n\tif args.Env != nil {\n\t\tenv, err = serde.EncodeEnvVar(args.Env)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding env vars: %w\", err)\n\t\t}\n\t}\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/pkg/rawobjects_function_args.go#L14-L50","documentation":"BamlFunctionArguments.encode() serializes the call's keyword arguments into protobuf map entries before crossing the FFI boundary. If serde.EncodeMapEntries fails, the error is wrapped with this message, meaning one or more argument values could not be encoded into the CFFI representation.","triggerScenarios":"Calling a generated BAML function (runtime.CallFunction / generated wrapper) while passing kwargs containing values not encodable by the serde layer — e.g. unsupported nested Go types, nil maps with wrong types, or values violating the serde schema.","commonSituations":"Passing a custom struct where the BAML function expects a primitive or BAML class; passing a map with non-string keys; version skew so the serde encoder doesn't recognize newer runtime types.","solutions":["Ensure function arguments match the types declared in the .baml function signature","Convert custom Go structs into BAML-compatible classes via a TypeBuilder or encode to map[string]any with string keys","Simplify the argument (log/marshal it to JSON first) to find which entry fails encoding","Update BAML Go bindings and runtime to matching versions"],"exampleFix":"// before\nkwargs := baml.NewKwargs(map[string]any{\"user\": myCustomGoStruct{}})\nres, err := runtime.CallFunction(ctx, \"Extract\", params, kwargs, nil)\n// after\nuserBytes, _ := json.Marshal(myCustomGoStruct{})\nvar userMap map[string]any\njson.Unmarshal(userBytes, &userMap)\nkwargs := baml.NewKwargs(map[string]any{\"user\": userMap})\nres, err := runtime.CallFunction(ctx, \"Extract\", params, kwargs, nil)","handlingStrategy":"validation","validationCode":"func validKwargs(kwargs map[string]any) bool {\n    for k, v := range kwargs {\n        if k == \"\" || v == nil { return false }\n        switch v.(type) {\n        case string, int, int64, float64, bool, []any, map[string]any:\n        default: return false\n        }\n    }\n    return true\n}","typeGuard":null,"tryCatchPattern":"res, err := runtime.CallFunction(ctx, \"Fn\", params, kwargs, nil)\nif err != nil && strings.Contains(err.Error(), \"encoding function arguments\") {\n    return fmt.Errorf(\"check kwargs types match .baml signature: %w\", err)\n}","preventionTips":["Pass only JSON-like values (string/number/bool/slice/map) as kwargs","Convert custom Go structs to map[string]any via JSON round-trip","Match argument names exactly to the .baml function signature"],"tags":["go","serialization","ffi","arguments"],"backgroundTag":"invalid-argument-format","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"}