Files
fn_registry/functions/ml/genconfig_json_marshal.md
T
egutierrez a802f59f55 chore: auto-commit (95 archivos)
- cmd/fn/doctor.go
- cmd/fn/main.go
- cpp/apps/primitives_gallery/playground/tables/CMakeLists.txt
- cpp/apps/primitives_gallery/playground/tables/data_table.cpp
- cpp/apps/primitives_gallery/playground/tables/data_table_logic.cpp
- cpp/apps/primitives_gallery/playground/tables/data_table_logic.h
- cpp/apps/primitives_gallery/playground/tables/self_test.cpp
- cpp/apps/primitives_gallery/playground/tables/tql.cpp
- cpp/apps/primitives_gallery/playground/tables/viz.cpp
- cpp/apps/primitives_gallery/playground/tables/viz.h
- ...

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 00:50:34 +02:00

3.1 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
genconfig_json_marshal function go ml 1.0.0 impure func GenconfigMarshal(cfg GenerationConfig) ([]byte, error) func GenconfigUnmarshal(data []byte) (GenerationConfig, error) Wrappers json.Marshal/Unmarshal para GenerationConfig con formato canonico (MarshalIndent 2 espacios). Garantiza roundtrip identico al Python: json.dumps(indent=2, sort_keys=False). Campos JSON en snake_case.
ml
json
marshal
unmarshal
serialization
generation
canonical
generation_config_go_ml
false error_go_core
encoding/json
name desc
cfg GenerationConfig a serializar. Campos omitempty (negative_prompt, loras, clip_skip) se omiten si son zero/nil/empty.
name desc
data JSON bytes a deserializar. Acepta formato compacto o con indent. Keys deben ser snake_case (negative_prompt, cfg_scale, model_type, etc.).
GenconfigMarshal: bytes JSON con indent 2 espacios, orden de campos segun declaracion del struct (prompt, negative_prompt, seed, steps, cfg_scale, sampler, width, height, model, loras, clip_skip). GenconfigUnmarshal: GenerationConfig poblado o error de parsing. true
roundtrip marshal unmarshal produce config igual
json cross-language snake_case keys se deserializan correctamente
functions/ml/genconfig_test.go functions/ml/genconfig_json_marshal.go

Ejemplo

cfg := ml.GenerationConfig{
    Prompt:   "a mountain at sunset",
    Seed:     1234,
    Steps:    30,
    CfgScale: 7.0,
    Sampler:  "euler",
    Width:    768,
    Height:   512,
    Model:    ml.ModelRef{Name: "sdxl-base", ModelType: "sdxl", Quantization: "fp16"},
}

b, err := ml.GenconfigMarshal(cfg)
// b == {
//   "prompt": "a mountain at sunset",
//   "seed": 1234,
//   ...
// }

cfg2, err := ml.GenconfigUnmarshal(b)
// cfg2 == cfg  (DeepEqual)

Notas

Formato canonico y compatibilidad con Python

GenconfigMarshal usa json.MarshalIndent(cfg, "", " "). El formato resultante es identico al que produce Python con model.model_dump_json() o json.dumps(data, indent=2) cuando sort_keys=False:

  • Keys en orden de declaracion del struct (no alfabetico).
  • Indent de 2 espacios, sin trailing whitespace.
  • Campos omitempty ausentes si zero: negative_prompt ausente si "", loras ausente si [], clip_skip ausente si nil.

Keys JSON (snake_case obligatorio)

Campo Go Key JSON
Prompt "prompt"
NegativePrompt "negative_prompt"
Seed "seed"
Steps "steps"
CfgScale "cfg_scale"
Sampler "sampler"
Width "width"
Height "height"
Model.ModelType "model_type"
Model.Quantization "quantization"
ClipSkip "clip_skip"

Por que impure

Los errores de json.Unmarshal son errores de parsing del input externo, no de I/O, pero se modelan como (T, error) para forzar manejo explicito en el caller. Marcado impure con error_type: error_go_core por convencion del registry.