tools: Ensure properties always exists in JSON Schema (#34015)

OpenAI API requires `properties` to be present, even if it's empty.

Release Notes:

- N/A
This commit is contained in:
Oleksiy Syvokon 2025-07-07 18:34:14 +03:00 committed by GitHub
parent 861ca05fb9
commit 966e75b610
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,10 +25,15 @@ fn preprocess_json_schema(json: &mut Value) -> Result<()> {
// `additionalProperties` defaults to `false` unless explicitly specified.
// This prevents models from hallucinating tool parameters.
if let Value::Object(obj) = json {
if let Some(Value::String(type_str)) = obj.get("type") {
if type_str == "object" && !obj.contains_key("additionalProperties") {
if matches!(obj.get("type"), Some(Value::String(s)) if s == "object") {
if !obj.contains_key("additionalProperties") {
obj.insert("additionalProperties".to_string(), Value::Bool(false));
}
// OpenAI API requires non-missing `properties`
if !obj.contains_key("properties") {
obj.insert("properties".to_string(), Value::Object(Default::default()));
}
}
}
Ok(())