diff --git a/control-plane/src/prompt.rs b/control-plane/src/prompt.rs
index 60c961a0..a7f1e02f 100644
--- a/control-plane/src/prompt.rs
+++ b/control-plane/src/prompt.rs
@@ -1,7 +1,8 @@
//! Supported text subset of the pinned Strata Qwen chat template.
-//! The no-tool path is byte-for-byte checked against upstream golden cases;
-//! tool definitions are adapted from Responses function objects.
-use serde_json::{Value, json};
+//! The no-tool path and a representative tool continuation are byte-for-byte
+//! checked against upstream golden cases. Tool definitions are adapted from
+//! Responses function objects.
+use serde_json::Value;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Role {
@@ -86,17 +87,22 @@ pub fn render_with_tools(
.get("name")
.and_then(Value::as_str)
.ok_or("tool needs a name")?;
- let description = tool
- .get("description")
- .and_then(Value::as_str)
- .unwrap_or("");
- let parameters = tool
- .get("parameters")
- .cloned()
- .unwrap_or_else(|| json!({"type":"object","properties":{}}));
- let wrapped = json!({"type":"function","function":{"name":name,"description":description,"parameters":parameters}});
prompt.push('\n');
- prompt.push_str(&wrapped.to_string());
+ prompt.push_str("{\"type\": \"function\", \"function\": {\"name\": ");
+ prompt.push_str(&serde_json::to_string(name).expect("string JSON"));
+ if let Some(description) = tool.get("description").and_then(Value::as_str) {
+ prompt.push_str(", \"description\": ");
+ prompt.push_str(&serde_json::to_string(description).expect("string JSON"));
+ }
+ if let Some(parameters) = tool.get("parameters") {
+ prompt.push_str(", \"parameters\": ");
+ write_template_json(parameters, &mut prompt);
+ }
+ if let Some(strict) = tool.get("strict") {
+ prompt.push_str(", \"strict\": ");
+ write_template_json(strict, &mut prompt);
+ }
+ prompt.push_str("}}");
}
prompt.push_str("\n\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n\n\n\nvalue_1\n\n\nThis is the value for the second parameter\nthat can span\nmultiple lines\n\n\n\n\n\nReminder:\n- Function calls MUST follow the specified format: an inner block must be nested within XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n");
}
@@ -130,10 +136,52 @@ pub fn render_with_tools(
Ok(prompt)
}
+fn write_template_json(value: &Value, output: &mut String) {
+ match value {
+ Value::Object(fields) => {
+ let mut keys = fields.keys().collect::>();
+ if fields.contains_key("type") {
+ let priority = |key: &str| match key {
+ "type" => 0,
+ "properties" => 1,
+ "required" => 2,
+ "items" => 3,
+ "enum" => 4,
+ "description" => 5,
+ "additionalProperties" => 6,
+ _ => 7,
+ };
+ keys.sort_by_key(|key| (priority(key), key.as_str()));
+ }
+ output.push('{');
+ for (index, key) in keys.into_iter().enumerate() {
+ if index > 0 {
+ output.push_str(", ");
+ }
+ output.push_str(&serde_json::to_string(key).expect("object key JSON"));
+ output.push_str(": ");
+ write_template_json(&fields[key], output);
+ }
+ output.push('}');
+ }
+ Value::Array(items) => {
+ output.push('[');
+ for (index, item) in items.iter().enumerate() {
+ if index > 0 {
+ output.push_str(", ");
+ }
+ write_template_json(item, output);
+ }
+ output.push(']');
+ }
+ _ => output.push_str(&value.to_string()),
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
- use serde_json::Value;
+ use serde_json::{Value, json};
#[test]
fn text_prompt_matches_upstream_golden_bytes() {
@@ -180,6 +228,48 @@ mod tests {
}
}
+ #[test]
+ fn representative_tool_prompt_matches_upstream_golden_bytes() {
+ let golden: Vec =
+ serde_json::from_str(include_str!("../../engine/strata/serve/chat_golden.json"))
+ .unwrap();
+ let case = golden
+ .iter()
+ .find(|case| case["name"] == "tool call and response")
+ .unwrap();
+ let function = &case["tools"][0]["function"];
+ let tools = [json!({
+ "type":"function", "name":function["name"], "description":function["description"],
+ "parameters":function["parameters"]
+ })];
+ let messages = [
+ Message {
+ role: Role::User,
+ content: "Weather in Oslo?",
+ reasoning: None,
+ },
+ Message {
+ role: Role::Assistant,
+ content: "\n\n\nOslo\n\n\n",
+ reasoning: None,
+ },
+ Message {
+ role: Role::User,
+ content: "\n{\"temp\": 3}\n",
+ reasoning: None,
+ },
+ Message {
+ role: Role::User,
+ content: "And in Bergen?",
+ reasoning: None,
+ },
+ ];
+ assert_eq!(
+ render_with_tools(&messages, Effort::XHigh, &tools).unwrap(),
+ case["rendered"]
+ );
+ }
+
#[test]
fn rejects_unrepresentable_message_order() {
let system = Message {
diff --git a/docs/build-validation.md b/docs/build-validation.md
index 12a1e9e7..0d39e77d 100644
--- a/docs/build-validation.md
+++ b/docs/build-validation.md
@@ -153,3 +153,4 @@ The test toolchain was installed into ignored `artifacts/tool-venv/` for this ru
- Follow-up terminal tests checked that worker errors produce `response.failed` and output-token exhaustion produces `response.incomplete` with `incomplete_details.reason=max_output_tokens`, in both the response trace and live SSE path. A length-limited response is no longer misreported as an HTTP gateway failure. All 73 Rust tests and Clippy with warnings denied passed locally.
- A Rust parser now accepts complete Qwen `` output in the buffered Responses path. It preserves literal closing-tag examples inside string parameter values, converts declared non-string JSON values, rejects unfinished/undeclared calls, and validates finished argument objects against the request's local JSON Schema without remote references. Function declarations with duplicate or unsafe names are rejected before generation. A synthetic HTTP test returned a `function_call` with exact string and integer values. All 78 Rust tests and Clippy with warnings denied passed at that step. Streaming tool calls and real-model validation remain open; the tool prompt is an adaptation of the upstream template rather than an established byte-for-byte parity result.
- The route now accepts a text-only `input` array for stateless continuation: earlier `function_call` items with JSON-object arguments must be paired with matching `function_call_output` text before a new generation. The Rust prompt includes the Qwen function and tool-response blocks; mismatched call IDs or incomplete pairs return a request error. A synthetic HTTP continuation reached the mock worker with the tool response intact and returned the next text answer. All 80 Rust tests, Clippy with warnings denied, and `git diff --check` passed. `previous_response_id` storage, multimodal items, reasoning replay, and real-model parity remain unimplemented.
+- The representative upstream `tool call and response` golden prompt now matches the Rust tool renderer byte for byte, including the tool schema JSON, function call block, and tool response turn. This checks one pinned case rather than arbitrary tool schemas or real-model behavior. All 81 Rust tests and Clippy with warnings denied passed after the formatting change.