Accept wrapped text content from LLM providers (#31048)

Some providers sometimes send `{ "type": "text", "text": ... }` instead
of just the text as a string. Now we accept those instead of erroring.

Release Notes:

- N/A
This commit is contained in:
Richard Feldman 2025-05-20 16:50:02 -04:00 committed by GitHub
parent 89700c3682
commit 4bb04cef9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 72 additions and 26 deletions

View file

@ -153,19 +153,29 @@ pub struct LanguageModelToolResult {
pub enum LanguageModelToolResultContent {
Text(Arc<str>),
Image(LanguageModelImage),
WrappedText(WrappedTextContent),
}
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq, Hash)]
pub struct WrappedTextContent {
#[serde(rename = "type")]
pub content_type: String,
pub text: Arc<str>,
}
impl LanguageModelToolResultContent {
pub fn to_str(&self) -> Option<&str> {
match self {
Self::Text(text) => Some(&text),
Self::Text(text) | Self::WrappedText(WrappedTextContent { text, .. }) => Some(&text),
Self::Image(_) => None,
}
}
pub fn is_empty(&self) -> bool {
match self {
Self::Text(text) => text.chars().all(|c| c.is_whitespace()),
Self::Text(text) | Self::WrappedText(WrappedTextContent { text, .. }) => {
text.chars().all(|c| c.is_whitespace())
}
Self::Image(_) => false,
}
}