{"record":{"id":"707e1c583879c1f5","repo":"puppetlabs/puppet","slug":"serialized-yaml-did-not-contain-a-valid-instance-o-707e1c","errorCode":null,"errorMessage":"Serialized YAML did not contain a valid instance of %{klass}","messagePattern":"Serialized YAML did not contain a valid instance of %(.+?)","errorType":"exception","errorClass":"Puppet::Network::FormatHandler::FormatError","httpStatus":null,"severity":"error","filePath":"lib/puppet/network/formats.rb","lineNumber":62,"sourceCode":"\n  def intern_multiple(klass, text)\n    data = Puppet::Util::Yaml.safe_load(text, allowed_yaml_classes)\n    unless data.respond_to?(:collect)\n      raise Puppet::Network::FormatHandler::FormatError, _(\"Serialized YAML did not contain a collection of instances when calling intern_multiple\")\n    end\n\n    data.collect do |datum|\n      data_to_instance(klass, datum)\n    end\n  rescue Puppet::Util::Yaml::YamlLoadError => e\n    raise Puppet::Network::FormatHandler::FormatError, _(\"Serialized YAML did not contain a valid instance of %{klass}: %{message}\") % { klass: klass, message: e.message }\n  end\n\n  def data_to_instance(klass, data)\n    return data if data.is_a?(klass)\n\n    unless data.is_a? Hash\n      raise Puppet::Network::FormatHandler::FormatError, _(\"Serialized YAML did not contain a valid instance of %{klass}\") % { klass: klass }\n    end\n\n    klass.from_data_hash(data)\n  end\n\n  def render(instance)\n    instance.to_yaml\n  end\n\n  # Yaml monkey-patches Array, so this works.\n  def render_multiple(instances)\n    instances.to_yaml\n  end\n\n  def supported?(klass)\n    true\n  end\nend","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/network/formats.rb#L44-L80","documentation":"After YAML loads, data_to_instance accepts a datum only if it already is an instance of the target class or is a Hash passable to klass.from_data_hash. Any other type — String, Symbol, Integer, Array for non-array classes — raises FormatError 'Serialized YAML did not contain a valid instance of %{klass}'. The document loaded fine; its elements are the wrong shape for the class being interned.","triggerScenarios":"Klass.convert_from(:yaml, '--- just-a-string'); intern_multiple over a sequence of scalars ('- 1', '- 2'); a producer serializing the wrong field (dumping obj.name instead of obj.to_data_hash).","commonSituations":"Test fixtures containing scalar YAML; producers dumping a field instead of the data hash; schema drift after from_data_hash changed across Puppet versions; consumers interning a class that does not match the document kind.","solutions":["Make each document/element a Hash matching the class's from_data_hash schema, or a real instance (serialize the object itself).","Fix the producer to dump the object's data hash, not a field of it.","Confirm the class being interned matches the document kind (a facts hash for Puppet::Node::Facts, etc.).","Update fixtures after any from_data_hash schema change."],"exampleFix":"# before — widgets.yaml contains '--- widget-42'\nWidget.convert_from(:yaml, File.read('widgets.yaml')) # not a Hash → error\n\n# after\n# widgets.yaml:\n# ---\n# id: 42\n# name: widget-42\nWidget.convert_from(:yaml, File.read('widgets.yaml'))","handlingStrategy":"validation","validationCode":"data = Puppet::Util::Yaml.safe_load(text, [])\nraise ArgumentError, 'document must be a data hash' unless data.is_a?(Hash) || data.is_a?(Klass)\nobj = Klass.convert_from(:yaml, text)","typeGuard":"def internable_yaml?(data, klass)\n  data.is_a?(klass) || data.is_a?(Hash)\nend","tryCatchPattern":"begin\n  obj = Klass.convert_from(:yaml, text)\nrescue Puppet::Network::FormatHandler::FormatError => e\n  raise unless e.message.include?('valid instance')\n  # shape mismatch — inspect the document and regenerate it as a data hash\nend","preventionTips":["Dump to_data_hash output (or the object itself), never scalar fields.","Assert fixtures are Hashes in test helpers.","Pin producer/consumer schemas across version bumps.","Shape-check untrusted YAML before interning."],"tags":["puppet","ruby","yaml","serialization","type-mismatch"],"backgroundTag":"yaml-type-mismatch","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}