fix(openai): accept null tool_call arguments in streaming chunks (#9035)
This commit is contained in:
@@ -32,10 +32,17 @@ type ToolCallData = HashMap<
|
|||||||
),
|
),
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
fn deserialize_null_default_string<'de, D>(deserializer: D) -> Result<String, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
Ok(Option::<String>::deserialize(deserializer)?.unwrap_or_default())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||||
struct DeltaToolCallFunction {
|
struct DeltaToolCallFunction {
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default, deserialize_with = "deserialize_null_default_string")]
|
||||||
arguments: String,
|
arguments: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2435,4 +2442,21 @@ data: [DONE]"#;
|
|||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delta_tool_call_function_accepts_null_arguments() {
|
||||||
|
let raw = r#"{"arguments":null}"#;
|
||||||
|
let parsed: DeltaToolCallFunction =
|
||||||
|
serde_json::from_str(raw).expect("null arguments must deserialize");
|
||||||
|
assert_eq!(parsed.arguments, "");
|
||||||
|
|
||||||
|
let raw = r#"{}"#;
|
||||||
|
let parsed: DeltaToolCallFunction =
|
||||||
|
serde_json::from_str(raw).expect("missing arguments must deserialize");
|
||||||
|
assert_eq!(parsed.arguments, "");
|
||||||
|
|
||||||
|
let raw = r#"{"arguments":"{\"k\":1}"}"#;
|
||||||
|
let parsed: DeltaToolCallFunction = serde_json::from_str(raw).unwrap();
|
||||||
|
assert_eq!(parsed.arguments, "{\"k\":1}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user