{"record":{"id":"51b89193d4137d7d","repo":"BoundaryML/baml","slug":"encoding-env-vars-w","errorCode":null,"errorMessage":"encoding env vars: %w","messagePattern":"encoding env vars: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/pkg/rawobjects_function_args.go","lineNumber":47,"sourceCode":"func (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\n\tvar collectors []*cffi.BamlObjectHandle\n\tif args.Collectors != nil {\n\t\tfor _, collector := range args.Collectors {\n\t\t\tif collector == nil {\n\t\t\t\treturn nil, fmt.Errorf(\"nil collector found in collectors\")\n\t\t\t}\n\t\t\tencodedCollector := raw_objects.EncodeRawObject(collector)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"encoding collector: %w\", err)\n\t\t\t}\n\t\t\tcollectors = append(collectors, encodedCollector)\n\t\t}\n\t}\n\n\tvar typeBuilder *cffi.BamlObjectHandle","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/pkg/rawobjects_function_args.go#L29-L65","documentation":"Environment variables passed in BamlFunctionArguments.Env are converted to CFFI HostEnvVar entries with serde.EncodeEnvVar. This error wraps any failure from that conversion, meaning one of the env vars (key or value) could not be represented in the FFI payload.","triggerScenarios":"Calling a BAML function while passing an Env map containing keys or values that fail serde encoding — e.g. non-string values in the env map, or a nil/incorrectly-typed Env value.","commonSituations":"Passing a map[string]any env map where some values are nil or non-strings (ints, bools) instead of map[string]string; injecting process environment with unusual entries; custom env built for client credential overrides.","solutions":["Pass env vars as a map[string]string (all string keys and values)","Convert non-string values with fmt.Sprintf/strconv before adding them to the env map","Skip nil or empty entries instead of including them in the map","Let the runtime inherit os.Environ() instead of hand-building the env map"],"exampleFix":"// before\nenv := map[string]any{\"TIMEOUT\": 30}\nres, _ := runtime.CallFunction(ctx, \"Fn\", params, kwargs, env)\n// after\nenv := map[string]string{\"TIMEOUT\": \"30\"}\nres, _ := runtime.CallFunction(ctx, \"Fn\", params, kwargs, env)","handlingStrategy":"validation","validationCode":"func validEnv(env map[string]string) bool {\n    for k, v := range env { if k == \"\" { return false } ; _ = v }\n    return true\n}","typeGuard":"func asStringMap(v any) (map[string]string, bool) { m, ok := v.(map[string]string); return m, ok }","tryCatchPattern":"res, err := runtime.CallFunction(ctx, \"Fn\", params, kwargs, env)\nif err != nil && strings.Contains(err.Error(), \"encoding env vars\") {\n    return fmt.Errorf(\"env map must be map[string]string: %w\", err)\n}","preventionTips":["Always type env args as map[string]string","Stringify numeric/bool env values before passing","Omit nil/empty entries rather than including them"],"tags":["go","serialization","ffi","env-vars"],"backgroundTag":"invalid-env-var-value","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"}