use guardrail_core::{
request::{
ChatMessage, ContentPart, GuardrailRequest, ImageUrl, MessageContent, Provider, Role,
},
GuardrailError,
};
use serde_json::{json, Value};
#[derive(Debug, Clone)]
pub struct ScannedBody {
body: Value,
pointers: Vec<Vec<String>>,
}
impl ScannedBody {
pub fn body(&self) -> &Value {
&self.body
}
pub fn apply(mut self, req: &GuardrailRequest) -> Result<Value, GuardrailError> {
if req.messages.len() != self.pointers.len() {
return Err(GuardrailError::Internal(format!(
"request has {} messages after the pipeline, expected {}",
req.messages.len(),
self.pointers.len()
)));
}
for (i, (message, pointers)) in req.messages.iter().zip(&self.pointers).enumerate() {
let texts = message_texts(&message.content);
if texts.len() != pointers.len() {
if pointers.is_empty() && texts.iter().all(|t| t.is_empty()) {
continue;
}
return Err(GuardrailError::Internal(format!(
"message {i} has {} text parts after the pipeline, expected {}",
texts.len(),
pointers.len()
)));
}
for (pointer, text) in pointers.iter().zip(texts) {
let slot = self.body.pointer_mut(pointer).ok_or_else(|| {
GuardrailError::Internal(format!("request body has no {pointer}"))
})?;
if slot.as_str() != Some(text) {
*slot = Value::String(text.to_string());
}
}
}
Ok(self.body)
}
}
fn message_texts(content: &MessageContent) -> Vec<&str> {
match content {
MessageContent::Text(text) => vec![text.as_str()],
MessageContent::Parts(parts) => parts
.iter()
.filter_map(|p| match p {
ContentPart::Text { text } => Some(text.as_str()),
ContentPart::ImageUrl { .. } => None,
})
.collect(),
}
}
pub fn parse_request(body: &[u8], provider: Provider) -> Result<GuardrailRequest, GuardrailError> {
parse_body(body, provider).map(|(req, _)| req)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Api {
ChatCompletions,
Messages,
Responses,
}
pub fn parse_api_body(
body: &[u8],
api: Api,
provider: Provider,
default_model: Option<&str>,
) -> Result<(GuardrailRequest, ScannedBody), GuardrailError> {
let value: Value = serde_json::from_slice(body)?;
let model = match (value.get("model").and_then(Value::as_str), default_model) {
(Some(model), _) => model.to_string(),
(None, Some(default)) => default.to_string(),
(None, None) if api == Api::Responses => String::new(),
(None, None) => {
return Err(GuardrailError::Internal(
"missing or invalid 'model' field".into(),
))
}
};
let stream = value
.get("stream")
.and_then(Value::as_bool)
.unwrap_or(false);
let extracted = match api {
Api::ChatCompletions | Api::Messages => extract_chat(&value, api)?,
Api::Responses => extract_responses(&value)?,
};
let mut extra = value.clone();
if let Value::Object(map) = &mut extra {
map.remove("model");
map.remove("messages");
map.remove("stream");
}
let (messages, pointers) = extracted.into_iter().map(Extracted::finish).unzip();
let req = GuardrailRequest {
id: uuid::Uuid::new_v4(),
received_at: std::time::SystemTime::now(),
messages,
provider,
model,
stream,
extra,
};
Ok((
req,
ScannedBody {
body: value,
pointers,
},
))
}
pub fn parse_body(
body: &[u8],
provider: Provider,
) -> Result<(GuardrailRequest, ScannedBody), GuardrailError> {
let api = if provider == Provider::Anthropic {
Api::Messages
} else {
Api::ChatCompletions
};
parse_api_body(body, api, provider, None)
}
fn extract_chat(value: &Value, api: Api) -> Result<Vec<Extracted>, GuardrailError> {
let messages = value
.get("messages")
.ok_or_else(|| GuardrailError::Internal("missing 'messages' field".into()))?
.as_array()
.ok_or_else(|| GuardrailError::Internal("'messages' must be an array".into()))?;
let mut extracted = Vec::with_capacity(messages.len() + 1);
if api == Api::Messages {
if let Some(system) = extract_anthropic_system(value.get("system"))? {
extracted.push(system);
}
}
for (i, message) in messages.iter().enumerate() {
extracted.push(extract_message(message, i, api)?);
}
Ok(extracted)
}
fn extract_responses(value: &Value) -> Result<Vec<Extracted>, GuardrailError> {
let mut extracted = Vec::new();
if let Some(instructions) = value.get("instructions").and_then(Value::as_str) {
let mut system = Extracted::new(Role::System);
system.plain_text("/instructions".into(), instructions);
extracted.push(system);
}
match value.get("input") {
None | Some(Value::Null) => {}
Some(Value::String(s)) => {
let mut user = Extracted::new(Role::User);
user.plain_text("/input".into(), s);
extracted.push(user);
}
Some(Value::Array(items)) => {
for (i, item) in items.iter().enumerate() {
if let Some(message) = extract_responses_item(item, i)? {
extracted.push(message);
}
}
}
Some(other) => {
return Err(GuardrailError::Internal(format!(
"'input' has unsupported JSON type: {other}"
)))
}
}
Ok(extracted)
}
fn extract_responses_item(item: &Value, i: usize) -> Result<Option<Extracted>, GuardrailError> {
let base = format!("/input/{i}");
let kind = item.get("type").and_then(Value::as_str);
match kind {
None | Some("message") => {
let role = item
.get("role")
.and_then(Value::as_str)
.ok_or_else(|| GuardrailError::Internal(format!("input[{i}] missing 'role'")))?;
let mut out = Extracted::new(role_from_str(role, &format!("input[{i}]"))?);
match item.get("content") {
Some(Value::String(s)) => out.plain_text(format!("{base}/content"), s),
Some(Value::Array(parts)) => {
for (j, part) in parts.iter().enumerate() {
let at = format!("{base}/content/{j}");
match part.get("type").and_then(Value::as_str) {
Some("input_text" | "output_text" | "text") => {
if let Some(text) = part.get("text").and_then(Value::as_str) {
out.text(format!("{at}/text"), text);
}
}
Some("refusal") => {
if let Some(text) = part.get("refusal").and_then(Value::as_str) {
out.text(format!("{at}/refusal"), text);
}
}
Some("input_image") => {
if let Some(url) = part.get("image_url").and_then(Value::as_str) {
out.segments.push(Segment::Image(ImageUrl {
url: url.to_string(),
detail: part
.get("detail")
.and_then(Value::as_str)
.map(String::from),
}));
}
}
_ => {}
}
}
}
_ => {}
}
Ok(Some(out))
}
Some("function_call") => {
let mut out = Extracted::new(Role::Assistant);
if let Some(args) = item.get("arguments").and_then(Value::as_str) {
out.text(format!("{base}/arguments"), args);
}
Ok(Some(out))
}
Some("function_call_output") => {
let mut out = Extracted::new(Role::Tool);
match item.get("output") {
Some(Value::String(s)) => out.text(format!("{base}/output"), s),
Some(Value::Array(parts)) => {
for (j, part) in parts.iter().enumerate() {
if let Some(text) = part.get("text").and_then(Value::as_str) {
out.text(format!("{base}/output/{j}/text"), text);
}
}
}
_ => {}
}
Ok(Some(out))
}
Some(_) => Ok(None),
}
}
struct Extracted {
role: Role,
segments: Vec<Segment>,
plain: bool,
}
enum Segment {
Text { pointer: String, text: String },
Image(ImageUrl),
}
impl Extracted {
fn new(role: Role) -> Self {
Self {
role,
segments: Vec::new(),
plain: false,
}
}
fn plain_text(&mut self, pointer: String, text: &str) {
self.plain = self.segments.is_empty();
self.text(pointer, text);
}
fn text(&mut self, pointer: String, text: &str) {
if !self.segments.is_empty() {
self.plain = false;
}
self.segments.push(Segment::Text {
pointer,
text: text.to_string(),
});
}
fn finish(self) -> (ChatMessage, Vec<String>) {
let mut pointers = Vec::new();
let mut parts = Vec::with_capacity(self.segments.len());
for segment in self.segments {
match segment {
Segment::Text { pointer, text } => {
pointers.push(pointer);
parts.push(ContentPart::Text { text });
}
Segment::Image(image_url) => parts.push(ContentPart::ImageUrl { image_url }),
}
}
let content = match parts.as_slice() {
[] => MessageContent::Text(String::new()),
[ContentPart::Text { text }] if self.plain => MessageContent::Text(text.clone()),
_ => MessageContent::Parts(parts),
};
(
ChatMessage {
role: self.role,
content,
},
pointers,
)
}
}
fn extract_anthropic_system(system: Option<&Value>) -> Result<Option<Extracted>, GuardrailError> {
let mut out = Extracted::new(Role::System);
match system {
None | Some(Value::Null) => {}
Some(Value::String(s)) => out.plain_text("/system".into(), s),
Some(Value::Array(blocks)) => {
for (j, block) in blocks.iter().enumerate() {
if let Some(text) = text_block(block) {
out.text(format!("/system/{j}/text"), text);
}
}
}
Some(other) => {
return Err(GuardrailError::Internal(format!(
"'system' has unsupported JSON type: {other}"
)))
}
}
Ok((!out.segments.is_empty()).then_some(out))
}
fn parse_role(message: &Value, i: usize) -> Result<Role, GuardrailError> {
let role = message
.get("role")
.and_then(Value::as_str)
.ok_or_else(|| GuardrailError::Internal(format!("messages[{i}] missing 'role' field")))?;
role_from_str(role, &format!("messages[{i}]"))
}
fn role_from_str(role: &str, location: &str) -> Result<Role, GuardrailError> {
match role {
"system" | "developer" => Ok(Role::System),
"user" => Ok(Role::User),
"assistant" => Ok(Role::Assistant),
"tool" | "function" => Ok(Role::Tool),
other => Err(GuardrailError::Internal(format!(
"{location} has unknown role '{other}'"
))),
}
}
fn extract_message(message: &Value, i: usize, api: Api) -> Result<Extracted, GuardrailError> {
let mut out = Extracted::new(parse_role(message, i)?);
let base = format!("/messages/{i}");
let anthropic = api == Api::Messages;
match message.get("content") {
None | Some(Value::Null) => {}
Some(Value::String(s)) => out.plain_text(format!("{base}/content"), s),
Some(Value::Array(parts)) => {
for (j, part) in parts.iter().enumerate() {
let at = format!("{base}/content/{j}");
if anthropic {
extract_anthropic_block(part, &at, &mut out);
} else {
extract_openai_part(part, &at, &mut out);
}
}
}
Some(other) => {
return Err(GuardrailError::Internal(format!(
"messages[{i}] 'content' has unsupported JSON type: {other}"
)))
}
}
if !anthropic {
if let Some(refusal) = message.get("refusal").and_then(Value::as_str) {
out.text(format!("{base}/refusal"), refusal);
}
if let Some(calls) = message.get("tool_calls").and_then(Value::as_array) {
for (k, call) in calls.iter().enumerate() {
if let Some(args) = call.pointer("/function/arguments").and_then(Value::as_str) {
out.text(format!("{base}/tool_calls/{k}/function/arguments"), args);
}
}
}
if let Some(args) = message
.pointer("/function_call/arguments")
.and_then(Value::as_str)
{
out.text(format!("{base}/function_call/arguments"), args);
}
}
Ok(out)
}
fn extract_openai_part(part: &Value, at: &str, out: &mut Extracted) {
match part.get("type").and_then(Value::as_str).unwrap_or("text") {
"text" => {
if let Some(text) = part.get("text").and_then(Value::as_str) {
out.text(format!("{at}/text"), text);
}
}
"refusal" => {
if let Some(text) = part.get("refusal").and_then(Value::as_str) {
out.text(format!("{at}/refusal"), text);
}
}
"image_url" => {
if let Some(url) = part.pointer("/image_url/url").and_then(Value::as_str) {
out.segments.push(Segment::Image(ImageUrl {
url: url.to_string(),
detail: part
.pointer("/image_url/detail")
.and_then(Value::as_str)
.map(String::from),
}));
}
}
_ => {}
}
}
fn extract_anthropic_block(block: &Value, at: &str, out: &mut Extracted) {
match block.get("type").and_then(Value::as_str) {
Some("text") => {
if let Some(text) = block.get("text").and_then(Value::as_str) {
out.text(format!("{at}/text"), text);
}
}
Some("tool_use") => {
if let Some(input) = block.get("input") {
extract_strings(input, &format!("{at}/input"), out);
}
}
Some("tool_result") => match block.get("content") {
Some(Value::String(s)) => out.text(format!("{at}/content"), s),
Some(Value::Array(inner)) => {
for (m, inner_block) in inner.iter().enumerate() {
if let Some(text) = text_block(inner_block) {
out.text(format!("{at}/content/{m}/text"), text);
}
}
}
_ => {}
},
Some("document")
if block.pointer("/source/type").and_then(Value::as_str) == Some("text") =>
{
if let Some(data) = block.pointer("/source/data").and_then(Value::as_str) {
out.text(format!("{at}/source/data"), data);
}
}
_ => {}
}
}
fn extract_strings(value: &Value, at: &str, out: &mut Extracted) {
match value {
Value::String(s) => out.text(at.to_string(), s),
Value::Array(items) => {
for (n, item) in items.iter().enumerate() {
extract_strings(item, &format!("{at}/{n}"), out);
}
}
Value::Object(map) => {
for (key, item) in map {
extract_strings(item, &format!("{at}/{}", escape_pointer(key)), out);
}
}
_ => {}
}
}
fn escape_pointer(key: &str) -> String {
key.replace('~', "~0").replace('/', "~1")
}
fn text_block(block: &Value) -> Option<&str> {
if block.get("type").and_then(Value::as_str) != Some("text") {
return None;
}
block.get("text").and_then(Value::as_str)
}
pub fn block_response_body(
reason: &str,
code: &guardrail_core::decision::BlockCode,
request_id: &str,
) -> Value {
json!({
"error": {
"message": reason,
"type": "guardrail_block",
"code": code.as_str(),
"guardrail_request_id": request_id,
}
})
}
#[cfg(test)]
mod tests {
use super::*;
fn openai(body: &[u8]) -> (GuardrailRequest, ScannedBody) {
parse_body(body, Provider::OpenAI).unwrap()
}
fn anthropic(body: &[u8]) -> (GuardrailRequest, ScannedBody) {
parse_body(body, Provider::Anthropic).unwrap()
}
fn redact_and_apply(req: GuardrailRequest, scanned: ScannedBody) -> Value {
let redactor = guardrail_classifiers::PiiRedactor::default();
let req = match redactor.redact_request(&req) {
Some((mutated, _)) => mutated,
None => req,
};
scanned.apply(&req).unwrap()
}
#[test]
fn test_parse_simple_openai_request() {
let (req, _) = openai(
br#"{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Hello!"}
],
"temperature": 0.5
}"#,
);
assert_eq!(req.model, "gpt-4o");
assert_eq!(req.messages.len(), 2);
assert_eq!(
req.messages[1].content,
MessageContent::Text("Hello!".into())
);
assert_eq!(req.extra["temperature"], 0.5);
assert!(req.extra.get("messages").is_none());
}
#[test]
fn test_parse_errors() {
for body in [
&br#"{"messages": []}"#[..],
br#"{"model": "gpt-4o"}"#,
b"not json",
br#"{"model": "gpt-4o", "messages": "not-an-array"}"#,
br#"{"model": "gpt-4o", "messages": [{"content": "Hi"}]}"#,
br#"{"model": "gpt-4o", "messages": [{"role": "alien", "content": "Hi"}]}"#,
br#"{"model": "gpt-4o", "messages": [{"role": "user", "content": 42}]}"#,
] {
assert!(
parse_body(body, Provider::OpenAI).is_err(),
"{}",
String::from_utf8_lossy(body)
);
}
}
#[test]
fn test_roles() {
let (req, _) = openai(
br#"{"model": "m", "messages": [
{"role": "system", "content": "a"},
{"role": "developer", "content": "b"},
{"role": "user", "content": "c"},
{"role": "assistant", "content": "d"},
{"role": "tool", "tool_call_id": "t", "content": "e"},
{"role": "function", "name": "f", "content": "f"}
]}"#,
);
let roles: Vec<Role> = req.messages.iter().map(|m| m.role.clone()).collect();
assert_eq!(
roles,
[
Role::System,
Role::System,
Role::User,
Role::Assistant,
Role::Tool,
Role::Tool
]
);
}
#[test]
fn test_message_without_content_is_allowed() {
let body = br#"{"model": "m", "messages": [{"role": "user"}]}"#;
let (req, scanned) = openai(body);
assert_eq!(req.messages[0].content, MessageContent::Text(String::new()));
let original: Value = serde_json::from_slice(body).unwrap();
assert_eq!(scanned.apply(&req).unwrap(), original);
}
#[test]
fn test_multipart_text_and_images_are_extracted() {
let (req, _) = openai(
br#"{"model": "gpt-4o", "messages": [{"role": "user", "content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/img.png", "detail": "high"}},
{"type": "input_audio", "input_audio": {"data": "AAAA", "format": "wav"}},
{"type": "video", "video": "x"}
]}]}"#,
);
let MessageContent::Parts(parts) = &req.messages[0].content else {
panic!("expected parts")
};
assert_eq!(parts.len(), 2);
assert_eq!(req.messages[0].content.as_text(), "What's in this image?");
assert!(
matches!(&parts[1], ContentPart::ImageUrl { image_url } if image_url.detail.as_deref() == Some("high"))
);
}
#[test]
fn test_tool_call_arguments_and_refusals_are_scanned() {
let (req, _) = openai(
br#"{"model": "m", "messages": [{"role": "assistant", "content": "calling",
"refusal": "no",
"tool_calls": [{"id": "c", "type": "function", "function": {"name": "f", "arguments": "{\"q\":1}"}}],
"function_call": {"name": "g", "arguments": "{\"r\":2}"}
}]}"#,
);
let texts = message_texts(&req.messages[0].content);
assert_eq!(texts, ["calling", "no", "{\"q\":1}", "{\"r\":2}"]);
}
#[test]
fn test_body_is_returned_unchanged_when_nothing_is_redacted() {
let body = br#"{
"model": "gpt-4o",
"messages": [
{"role": "user", "name": "alice", "content": "Hi"},
{"role": "assistant", "content": null, "tool_calls": [
{"id": "c1", "type": "function", "function": {"name": "f", "arguments": "{}"}}
]},
{"role": "tool", "tool_call_id": "c1", "content": "ok"}
],
"tools": [{"type": "function", "function": {"name": "f"}}],
"temperature": 0.7
}"#;
let (req, scanned) = openai(body);
let original: Value = serde_json::from_slice(body).unwrap();
let out = redact_and_apply(req, scanned);
assert_eq!(out, original);
assert!(out.get("stream").is_none(), "no stream key is added");
}
#[test]
fn test_redactions_land_on_their_own_pointers() {
let (req, scanned) = openai(
br#"{"model": "m", "messages": [{"role": "assistant", "content": [
{"type": "text", "text": "a@example.com"},
{"type": "image_url", "image_url": {"url": "https://example.com/x.png"}},
{"type": "text", "text": "clean"}
], "tool_calls": [{"id": "c", "type": "function",
"function": {"name": "f", "arguments": "{\"to\":\"b@example.com\"}"}}]}]}"#,
);
let out = redact_and_apply(req, scanned);
let msg = &out["messages"][0];
assert_eq!(msg["content"][0]["text"], "[EMAIL]");
assert_eq!(
msg["content"][1]["image_url"]["url"],
"https://example.com/x.png"
);
assert_eq!(msg["content"][2]["text"], "clean");
assert_eq!(
msg["tool_calls"][0]["function"]["arguments"],
"{\"to\":\"[EMAIL]\"}"
);
}
#[test]
fn test_anthropic_system_string_is_scanned_and_written_back() {
let (req, scanned) = anthropic(
br#"{"model": "m", "system": "mail bob@example.com", "messages": [{"role": "user", "content": "Hi"}]}"#,
);
assert_eq!(req.messages.len(), 2);
assert_eq!(req.messages[0].role, Role::System);
let out = redact_and_apply(req, scanned);
assert_eq!(out["system"], "mail [EMAIL]");
assert_eq!(out["messages"].as_array().unwrap().len(), 1);
}
#[test]
fn test_anthropic_system_blocks_keep_cache_control() {
let (req, scanned) = anthropic(
br#"{"model": "m", "system": [
{"type": "text", "text": "You are a support bot."},
{"type": "text", "text": "mail bob@example.com", "cache_control": {"type": "ephemeral"}}
], "messages": [{"role": "user", "content": "Hi"}]}"#,
);
assert!(req.all_text().contains("bob@example.com"));
let out = redact_and_apply(req, scanned);
assert_eq!(out["system"][0]["text"], "You are a support bot.");
assert_eq!(out["system"][1]["text"], "mail [EMAIL]");
assert_eq!(out["system"][1]["cache_control"]["type"], "ephemeral");
}
#[test]
fn test_anthropic_blocks() {
let (req, scanned) = anthropic(
br#"{"model": "m", "messages": [
{"role": "user", "content": [
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "iVBOR"}},
{"type": "document", "source": {"type": "text", "media_type": "text/plain", "data": "doc e@example.com"}},
{"type": "tool_result", "tool_use_id": "t", "content": "result f@example.com"}
]},
{"role": "assistant", "content": [
{"type": "thinking", "thinking": "g@example.com", "signature": "sig"},
{"type": "tool_use", "id": "t", "name": "n", "input": {"a/b": ["h@example.com", 3], "c~d": {"e": "x"}}}
]}
]}"#,
);
assert_eq!(
message_texts(&req.messages[0].content),
["doc e@example.com", "result f@example.com"]
);
assert_eq!(
message_texts(&req.messages[1].content),
["h@example.com", "x"]
);
let out = redact_and_apply(req, scanned);
let user = &out["messages"][0]["content"];
assert_eq!(user[1]["source"]["data"], "doc [EMAIL]");
assert_eq!(user[2]["content"], "result [EMAIL]");
let assistant = &out["messages"][1]["content"];
assert_eq!(assistant[0]["thinking"], "g@example.com");
assert_eq!(assistant[1]["input"]["a/b"][0], "[EMAIL]");
assert_eq!(assistant[1]["input"]["a/b"][1], 3);
assert_eq!(assistant[1]["input"]["c~d"]["e"], "x");
}
#[test]
fn test_apply_refuses_a_request_that_no_longer_lines_up() {
let (mut req, scanned) = openai(
br#"{"model": "m", "messages": [{"role": "user", "content": [
{"type": "text", "text": "a"}, {"type": "text", "text": "b"}
]}]}"#,
);
req.messages[0].content = MessageContent::Text("ab".into());
assert!(scanned.clone().apply(&req).is_err());
req.messages.clear();
assert!(scanned.apply(&req).is_err());
}
#[test]
fn test_block_response_body_shape() {
let body = block_response_body(
"test reason",
&guardrail_core::decision::BlockCode::PromptInjection,
"req-abc",
);
assert_eq!(body["error"]["message"], "test reason");
assert_eq!(body["error"]["code"], "prompt_injection");
assert_eq!(body["error"]["guardrail_request_id"], "req-abc");
assert_eq!(body["error"]["type"], "guardrail_block");
}
#[test]
fn test_responses_body() {
let body = br#"{
"model": "gpt-4.1",
"instructions": "Be brief.",
"input": [
{"role": "developer", "content": "dev a@example.com"},
{"type": "message", "role": "user", "content": [
{"type": "input_text", "text": "hi b@example.com"},
{"type": "input_image", "image_url": "https://example.com/i.png"}
]},
{"type": "reasoning", "id": "rs_1", "summary": []},
{"type": "function_call", "call_id": "c", "name": "f", "arguments": "{\"to\":\"c@example.com\"}"},
{"type": "function_call_output", "call_id": "c", "output": "d@example.com"}
],
"store": false
}"#;
let (req, scanned) = parse_api_body(body, Api::Responses, Provider::OpenAI, None).unwrap();
let roles: Vec<Role> = req.messages.iter().map(|m| m.role.clone()).collect();
assert_eq!(
roles,
[
Role::System,
Role::System,
Role::User,
Role::Assistant,
Role::Tool
]
);
let out = redact_and_apply(req, scanned);
assert_eq!(out["instructions"], "Be brief.");
assert_eq!(out["input"][0]["content"], "dev [EMAIL]");
assert_eq!(out["input"][1]["content"][0]["text"], "hi [EMAIL]");
assert_eq!(
out["input"][1]["content"][1]["image_url"],
"https://example.com/i.png"
);
assert_eq!(out["input"][2]["type"], "reasoning");
assert_eq!(out["input"][3]["arguments"], "{\"to\":\"[EMAIL]\"}");
assert_eq!(out["input"][4]["output"], "[EMAIL]");
assert_eq!(out["store"], false);
}
#[test]
fn test_model_defaults() {
let azure = br#"{"messages": [{"role": "user", "content": "hi"}]}"#;
assert!(parse_body(azure, Provider::Azure).is_err());
let (req, scanned) =
parse_api_body(azure, Api::ChatCompletions, Provider::Azure, Some("prod")).unwrap();
assert_eq!(req.model, "prod");
assert!(scanned.apply(&req).unwrap().get("model").is_none());
let (req, _) = parse_api_body(
br#"{"previous_response_id": "r"}"#,
Api::Responses,
Provider::OpenAI,
None,
)
.unwrap();
assert!(req.messages.is_empty());
}
#[test]
fn test_stream_flag() {
let (req, _) = openai(br#"{"model": "m", "messages": [], "stream": true}"#);
assert!(req.stream);
}
}