{"id":7692,"library":"ruamel-yaml-string","title":"ruamel.yaml.string","description":"ruamel.yaml.string is a plugin for the `ruamel.yaml` YAML parser/emitter library. It extends the `ruamel.yaml.YAML` instance by adding `dump_to_string` (and its alias `dumps`) methods, allowing users to serialize YAML documents directly into a Python string instead of writing to a file-like object. The current version is 0.1.1, last updated in May 2023, and its release cadence is tied to `ruamel.yaml` and bug fixes.","status":"active","version":"0.1.1","language":"en","source_language":"en","source_url":"https://pypi.org/project/ruamel.yaml.string/","tags":["YAML","ruamel.yaml","dump","string","serialization","plugin"],"install":[{"cmd":"pip install ruamel.yaml.string","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"This library is a plugin for ruamel.yaml and depends on its core functionality.","package":"ruamel.yaml","optional":false}],"imports":[{"note":"The `YAML` class is part of the main `ruamel.yaml` library. The `ruamel.yaml.string` plugin adds methods to its instances, but does not provide its own `YAML` class for direct import.","wrong":"from ruamel.yaml.string import YAML","symbol":"YAML","correct":"from ruamel.yaml import YAML"}],"quickstart":{"code":"from ruamel.yaml import YAML\nimport sys\n\n# Instantiate YAML with the 'string' type to enable dump_to_string/dumps\nyaml = YAML(typ=['rt', 'string'])\n\ndata = {\n    'name': 'John Doe',\n    'age': 30,\n    'hobbies': ['reading', 'hiking', 'coding'],\n    'address': {\n        'street': '123 Main St',\n        'city': 'Anytown'\n    }\n}\n\n# Dump to string using dump_to_string\nyaml_string = yaml.dump_to_string(data)\nprint('YAML as string (dump_to_string):')\nprint(yaml_string)\n\n# Dump to string using dumps (alias for dump_to_string)\nyaml_string_alias = yaml.dumps(data, add_final_eol=True)\nprint('\\nYAML as string (dumps with final EOL):')\nprint(yaml_string_alias)","lang":"python","description":"This quickstart demonstrates how to initialize `ruamel.yaml.YAML` with `typ=['rt', 'string']` to enable the `dump_to_string` and `dumps` methods, which return the YAML document as a Python string. It also shows the `add_final_eol` parameter for controlling the trailing newline behavior."},"warnings":[{"fix":"To add a final newline, use `yaml.dump_to_string(data, add_final_eol=True)` or `yaml.dumps(data, add_final_eol=True)`.","message":"The `dump_to_string` and `dumps` methods do not add a final newline to the output string by default. This differs from `PyYAML.dump` and `print()` behavior, which often add a newline.","severity":"gotcha","affected_versions":"0.1.0+"},{"fix":"Prefer `yaml.dump(data, output_stream)` for performance-critical scenarios where the output stream is known.","message":"For maximum efficiency when dumping YAML, especially for large documents, directly writing to a stream (e.g., `sys.stdout` or a file handle) using `yaml.dump(data, sys.stdout)` is more efficient than first generating a string with `yaml.dumps(data)` and then printing it.","severity":"gotcha","affected_versions":"0.1.0+"},{"fix":"Ensure your `YAML` instance is created as `yaml = YAML(typ=['rt', 'string'])` to enable the string dumping functionality.","message":"The `dump_to_string` and `dumps` methods are only available on a `ruamel.yaml.YAML` instance when initialized with `typ=['rt', 'string']`. If `typ` is not specified or does not include `'string'`, these methods will not be available.","severity":"gotcha","affected_versions":"0.1.0+"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Initialize your YAML object with `yaml = YAML(typ=['rt', 'string'])`.","cause":"The `ruamel.yaml.YAML` instance was not initialized with `typ=['rt', 'string']` to enable the plugin's methods.","error":"AttributeError: 'YAML' object has no attribute 'dump_to_string'"},{"fix":"If you want a string, use `yaml_string = yaml.dumps(data)`. If you want to dump to a file, use `yaml.dump(data, file_handle)`.","cause":"Attempting to pass a `data` object and a stream *and* expecting a string return. The standard `yaml.dump()` method writes to a stream and returns `None`, while `dump_to_string`/`dumps` return a string.","error":"TypeError: dump() takes 2 positional arguments but 3 were given"}]}