Skip to main content

extract_messages

Function extract_messages 

Source
pub fn extract_messages(
    input: &Value,
    system_prompt: Option<&str>,
) -> Vec<Message>
Expand description

Extract typed messages from a graph state JSON, with an optional system prompt prepended.

This replaces the common 8-line pattern:

let messages_json = input.get("messages")
    .and_then(|m| m.as_array()).cloned().unwrap_or_default();
let mut typed_messages = vec![Message::system("...")];
for msg in &messages_json {
    if let Ok(m) = serde_json::from_value::<Message>(msg.clone()) {
        typed_messages.push(m);
    }
}

With:

let messages = extract_messages(&input, Some("You are a helpful assistant."));