---
title: "Migrate from serde_yaml to oneyaml"
url: https://recursiveai.org/oneyaml/articles/migrate-from-serde-yaml/
description: "Replace serde_yaml in Rust without changing your Serde structs. Compare output, check compatibility settings, and catch changes before shipping."
date: 2026-11-27
author: oneyaml team
---

# Migrate from serde_yaml to oneyaml

Replace serde_yaml in Rust without changing your Serde structs. Compare output, check compatibility settings, and catch changes before shipping.

November 27, 2026 · oneyaml team

**oneyaml** is a free, open-source YAML library for Rust from Recursive AI, licensed MIT or Apache-2.0. [crates.io](https://crates.io/crates/oneyaml/1.2.0) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-rust/tree/14445615f5a5b2895fdaa1da7719584ef15d9d6b)

`cargo add oneyaml@=1.2.0`

Keep your Serde derives and swap the crate for typed reads and writes. To check values, output, and errors before dropping [serde_yaml 0.9.34+deprecated](https://docs.rs/serde_yaml/0.9.34+deprecated/serde_yaml/), put these entries under `[dependencies]` in a Rust binary crate.

*Cargo.toml · TOML*

```
oneyaml = "=1.2.0"
serde = { version = "=1.0.228", features = ["derive"] }
serde_yaml = "=0.9.34"
```

For the same `Config` and `input`, replace the first line with the second:

*Before → after · Rust*

```
let config: Config = serde_yaml::from_str(input)?; let yaml = serde_yaml::to_string(&config)?;
let config: Config = oneyaml::from_str(input)?; let yaml = oneyaml::to_string(&config)?;
```

Save the following as `src/main.rs` and run `cargo run`.

*src/main.rs · Rust*

```
use serde::{Deserialize, Serialize};
 
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Config {
    tags: Vec<String>,
}
 
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input = "tags:\n- api\n";
    let old: Config = serde_yaml::from_str(input)?;
    let new: Config = oneyaml::from_str(input)?;
    let compat = oneyaml::Compat::serde_yaml();
    assert_eq!(old, new);
    println!("serde_yaml: {:?}", serde_yaml::to_string(&old)?);
    println!("oneyaml:    {:?}", oneyaml::to_string(&new)?);
    println!("compat:     {:?}", oneyaml::to_string_with(&new, &compat)?);
 
    let input = "mask: 0b1010\n";
    let old: serde_yaml::Value = serde_yaml::from_str(input)?;
    let new: oneyaml::Value = oneyaml::from_str(input)?;
    let migrated: oneyaml::Value = oneyaml::from_str_with(input, &compat)?;
    println!("binary mask: {:?} / {:?} / {:?}",
        old["mask"].as_u64(), new["mask"].as_str(), migrated["mask"].as_u64());
 
    let duplicates = "port: 80\nport: 443\n";
    let old_rejects = serde_yaml::from_str::<serde_yaml::Value>(duplicates).is_err();
    let error = oneyaml::from_str::<oneyaml::Value>(duplicates).unwrap_err();
    let migrated: oneyaml::Value = oneyaml::from_str_with(duplicates, &compat)?;
    println!("duplicates: old rejects = {old_rejects}, new = {}, compat = {}",
        error.code(), migrated["port"].as_u64().unwrap());
    Ok(())
}
```

*Output*

```
serde_yaml: "tags:\n- api\n"
oneyaml:    "tags:\n  - api\n"
compat:     "tags:\n- api\n"
binary mask: Some(10) / Some("0b1010") / Some(10)
duplicates: old rejects = true, new = Y1013, compat = 443
```

The two typed reads produce the same `Config`. The default writer indents the list, while the compatibility writer matches the old output for this fixture. The binary mask becomes a string under the default settings. The preset restores the integer but also accepts the second `port` value, which both default reads rejected. `Y1013` is oneyaml’s duplicate-key error code. Pass the compatibility settings only to the calls whose old behavior you need to keep.

## Map the calls before swapping imports

Use the [serde_yaml 0.9.34 API](https://docs.rs/serde_yaml/0.9.34+deprecated/serde_yaml/) and [oneyaml 1.2.0 migration map](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/docs/migrating-from-serde-yaml.md#L31-L75) to check any call that does more than a typed string round trip.

| serde_yaml | oneyaml 1.2.0 |
|---|---|
| `from_str`, `from_reader` | Same entry-point names. Use the `_with` variants to pass settings. |
| `from_slice` into a borrowed type | `from_slice` requires `DeserializeOwned`. For borrowed fields in UTF-8 input, validate with `std::str::from_utf8` and then use `from_str`. |
| `to_string`, `to_writer` | Same names. Use `to_string_with` or `to_writer_with` and the compatibility settings where old layout matters. Check golden files; this is not a byte-for-byte guarantee for all inputs. |
| `Value`, `from_value`, `to_value` | Same names under the new crate. The `Value` types are distinct; switch imports and check scalar and tag behavior. |
| `value::Tag`, `value::TaggedValue` | `oneyaml::Tag`, `oneyaml::TaggedValue`. |
| `Deserializer::from_str` for a stream of documents | Same iteration entry point. Test the complete stream, not just the first document. |
| `Error`, `Location` | Same type names under the new crate. Use `Error::code()` for assertions on oneyaml errors rather than matching the displayed sentence. |

## A checklist for production fixtures

1. **Start with default settings.** Exercise real configs with missing fields, quoted scalars, and numeric spellings. Include both typed reads and `Value` reads if your app uses them. Include tagged enum variants with scalar payloads: the [preset](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/docs/migrating-from-serde-yaml.md#L83-L117) opts into resolving those payloads for Serde.
2. **Keep the duplicate-key policy explicit.** Leave defaults on if you want duplicate keys rejected. If you need the rest of the preset, override its `duplicate_keys` setting with `DuplicateKeys::Error` or inspect warnings with `from_str_with_diagnostics` if you intentionally accept last-wins behavior.
3. **Review merge and alias handling separately.** Include a file that uses `<<` if your old path expanded merges. The compatibility preset is not the switch for merge expansion. See the [merge-key migration notes](https://recursiveai.org/oneyaml/rust/docs/migrating-from-serde-yaml/#merge-keys-) for the explicit settings and value-tree operation.
4. **Test output where it is consumed.** Compare bytes for golden files or signed artifacts and read back the emitted YAML to test actual values. For edits that must keep existing comments, use the [document API](https://recursiveai.org/oneyaml/rust/docs/) rather than rewriting a Serde struct.

Remove the old dependency once callers and fixtures agree. If you are still choosing a replacement rather than porting code, start with [the serde_yaml deprecation guide](https://recursiveai.org/oneyaml/articles/serde-yaml-deprecated/).
