rig icon indicating copy to clipboard operation
rig copied to clipboard

refactor: reasoning should not be Vec<String>

Open joshua-mo-143 opened this issue 6 months ago • 2 comments

  • [x] I have looked for existing issues (including closed) about this

Feature Request

Reasoning was recently made Vec<String> to accommodate for the fact that OpenAI seems to have made a very strange choice in making reasoning an array of strings.

We should probably figure out how to deal with this issue as while it does now work, it is also a bit of an unnecessary DX papercut.

Motivation

Improve DX

joshua-mo-143 avatar Aug 11 '25 00:08 joshua-mo-143

Something like this would be probably enough?

fn deserialize_string_or_vec<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum StringOrVec {
        String(String),
        Vec(Vec<String>),
    }
    
    match StringOrVec::deserialize(deserializer)? {
        StringOrVec::String(s) => Ok(s),
        StringOrVec::Vec(v) => Ok(v.join("")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[derive(Deserialize)]
    struct MyStruct {
        #[serde(deserialize_with = "deserialize_string_or_vec")]
        field: String,
    }
    
    #[test]
    fn test_string_input() {
        let json = r#"{"field": "hello world"}"#;
        let result: MyStruct = serde_json::from_str(json).unwrap();
        assert_eq!(result.field, "hello world");
    }
    
    #[test]
    fn test_vec_input() {
        let json = r#"{"field": ["hello", " ", "world"]}"#;
        let result: MyStruct = serde_json::from_str(json).unwrap();
        assert_eq!(result.field, "hello world");
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=bd9697b1fc51a199dc6ff87b7c776b0d

quangIO avatar Aug 11 '25 04:08 quangIO