Fix openai input_text type compatibility (#11935)
This commit is contained in:
@@ -749,29 +749,41 @@ impl crate::routers::RouterTrait for OpenAIRouter {
|
|||||||
] {
|
] {
|
||||||
obj.remove(key);
|
obj.remove(key);
|
||||||
}
|
}
|
||||||
// XAI doesn't support the OPENAI item type input: https://platform.openai.com/docs/api-reference/responses/create#responses-create-input-input-item-list-item
|
// XAI (Grok models) requires special handling of input items
|
||||||
// To Achieve XAI compatibility, strip extra fields from input messages (id, status)
|
// Check if model is a Grok model
|
||||||
// XAI doesn't support output_text as type for content with role of assistant
|
let is_grok_model = obj
|
||||||
// so normalize content types: output_text -> input_text
|
.get("model")
|
||||||
if let Some(input_arr) = obj.get_mut("input").and_then(Value::as_array_mut) {
|
.and_then(|v| v.as_str())
|
||||||
for item_obj in input_arr.iter_mut().filter_map(Value::as_object_mut) {
|
.map(|m| m.starts_with("grok"))
|
||||||
// Remove fields not universally supported
|
.unwrap_or(false);
|
||||||
item_obj.remove("id");
|
|
||||||
item_obj.remove("status");
|
|
||||||
|
|
||||||
// Normalize content types to input_text (xAI compatibility)
|
if is_grok_model {
|
||||||
if let Some(content_arr) =
|
// XAI doesn't support the OPENAI item type input: https://platform.openai.com/docs/api-reference/responses/create#responses-create-input-input-item-list-item
|
||||||
item_obj.get_mut("content").and_then(Value::as_array_mut)
|
// To Achieve XAI compatibility, strip extra fields from input messages (id, status)
|
||||||
{
|
// XAI doesn't support output_text as type for content with role of assistant
|
||||||
for content_obj in content_arr.iter_mut().filter_map(Value::as_object_mut) {
|
// so normalize content types: output_text -> input_text
|
||||||
// Change output_text to input_text
|
if let Some(input_arr) = obj.get_mut("input").and_then(Value::as_array_mut) {
|
||||||
if content_obj.get("type").and_then(Value::as_str)
|
for item_obj in input_arr.iter_mut().filter_map(Value::as_object_mut) {
|
||||||
== Some("output_text")
|
// Remove fields not universally supported
|
||||||
|
item_obj.remove("id");
|
||||||
|
item_obj.remove("status");
|
||||||
|
|
||||||
|
// Normalize content types to input_text (xAI compatibility)
|
||||||
|
if let Some(content_arr) =
|
||||||
|
item_obj.get_mut("content").and_then(Value::as_array_mut)
|
||||||
|
{
|
||||||
|
for content_obj in
|
||||||
|
content_arr.iter_mut().filter_map(Value::as_object_mut)
|
||||||
{
|
{
|
||||||
content_obj.insert(
|
// Change output_text to input_text
|
||||||
"type".to_string(),
|
if content_obj.get("type").and_then(Value::as_str)
|
||||||
Value::String("input_text".to_string()),
|
== Some("output_text")
|
||||||
);
|
{
|
||||||
|
content_obj.insert(
|
||||||
|
"type".to_string(),
|
||||||
|
Value::String("input_text".to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user