fix(databricks): drop thinking blocks bound to a different prefix (#13551)
Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: Lifei Zhou <lifei@squareup.com> (cherry picked from commit 73b6e76c99751d0568730c84d0bc23e087b191b8)
This commit is contained in:
@@ -449,7 +449,7 @@ impl Provider for AnthropicProvider {
|
||||
}
|
||||
}
|
||||
|
||||
fn beta_header_value(
|
||||
pub(crate) fn beta_header_value(
|
||||
api_client: &ApiClient,
|
||||
model_config: &ModelConfig,
|
||||
payload: &Value,
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use crate::formats::anthropic::{AnthropicFormatOptions, ANTHROPIC_PROVIDER_NAME};
|
||||
use crate::anthropic::beta_header_value;
|
||||
use crate::formats::anthropic::{
|
||||
block_binding_behavior, AnthropicFormatOptions, PrefixMismatchBehavior, ANTHROPIC_PROVIDER_NAME,
|
||||
};
|
||||
use crate::formats::openai::{self, extract_reasoning_effort, is_openai_responses_model};
|
||||
use crate::http_status::{read_error_body, read_json_response};
|
||||
use crate::images::ImageFormat;
|
||||
@@ -107,6 +110,11 @@ pub struct DatabricksV2Provider {
|
||||
refresh_hook: Option<DatabricksRefreshHook>,
|
||||
#[serde(skip)]
|
||||
gateway_path: String,
|
||||
/// Models that rejected a replayed thinking block as bound to a different
|
||||
/// prefix. Only those get `block_binding`: older Claude models on the gateway
|
||||
/// reject the field itself.
|
||||
#[serde(skip)]
|
||||
binding_models: Arc<Mutex<HashSet<String>>>,
|
||||
}
|
||||
|
||||
impl DatabricksV2Provider {
|
||||
@@ -154,6 +162,7 @@ impl DatabricksV2Provider {
|
||||
token_cache,
|
||||
refresh_hook,
|
||||
gateway_path: DATABRICKS_V2_DEFAULT_GATEWAY_PATH.to_string(),
|
||||
binding_models: Arc::default(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -496,6 +505,48 @@ impl DatabricksV2Provider {
|
||||
}))
|
||||
}
|
||||
|
||||
fn anthropic_messages_payload(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
drop_mismatched_thinking: bool,
|
||||
) -> Result<Value> {
|
||||
let options = if drop_mismatched_thinking {
|
||||
AnthropicFormatOptions::native()
|
||||
} else {
|
||||
AnthropicFormatOptions::default()
|
||||
};
|
||||
let mut payload = anthropic::create_request(
|
||||
ANTHROPIC_PROVIDER_NAME,
|
||||
model_config,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
options,
|
||||
)?;
|
||||
payload["stream"] = Value::Bool(true);
|
||||
Ok(payload)
|
||||
}
|
||||
|
||||
async fn post_anthropic_messages(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
payload: &Value,
|
||||
) -> Result<reqwest::Response, ProviderError> {
|
||||
let beta_header = beta_header_value(&self.api_client, model_config, payload);
|
||||
let path = self.route_path(DatabricksV2Route::AnthropicMessages);
|
||||
self.with_retry(|| async {
|
||||
let mut request = self.api_client.request(&path).model_headers(model_config)?;
|
||||
if let Some(beta) = &beta_header {
|
||||
request = request.header("anthropic-beta", beta)?;
|
||||
}
|
||||
handle_status(request.streaming(true).response_post(payload).await?).await
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn stream_anthropic_messages(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
@@ -503,32 +554,35 @@ impl DatabricksV2Provider {
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
let mut payload = anthropic::create_request(
|
||||
ANTHROPIC_PROVIDER_NAME,
|
||||
model_config,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
AnthropicFormatOptions::default(),
|
||||
)?;
|
||||
payload["stream"] = Value::Bool(true);
|
||||
let model = &model_config.model_name;
|
||||
let binds = self.binding_models.lock().unwrap().contains(model);
|
||||
let payload =
|
||||
self.anthropic_messages_payload(model_config, system, messages, tools, binds)?;
|
||||
let mut log = start_log(model_config, &payload)?;
|
||||
|
||||
let response = self
|
||||
.with_retry(|| async {
|
||||
let resp = self
|
||||
.api_client
|
||||
.request(&self.route_path(DatabricksV2Route::AnthropicMessages))
|
||||
.model_headers(model_config)?
|
||||
.streaming(true)
|
||||
.response_post(&payload)
|
||||
.await?;
|
||||
handle_status(resp).await
|
||||
})
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
let response = match self.post_anthropic_messages(model_config, &payload).await {
|
||||
Err(ProviderError::RequestFailed(message))
|
||||
if !binds && Self::is_thinking_binding_error(&message) =>
|
||||
{
|
||||
let retry =
|
||||
self.anthropic_messages_payload(model_config, system, messages, tools, true)?;
|
||||
if block_binding_behavior(&retry) != Some(PrefixMismatchBehavior::DropBlock) {
|
||||
Err(ProviderError::RequestFailed(message))
|
||||
} else {
|
||||
let _ = log.error(&message);
|
||||
log = start_log(model_config, &retry)?;
|
||||
self.post_anthropic_messages(model_config, &retry)
|
||||
.await
|
||||
.inspect(|_| {
|
||||
self.binding_models.lock().unwrap().insert(model.clone());
|
||||
})
|
||||
}
|
||||
}
|
||||
other => other,
|
||||
}
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
|
||||
let stream = response.bytes_stream().map_err(io::Error::other);
|
||||
|
||||
@@ -546,6 +600,10 @@ impl DatabricksV2Provider {
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
fn is_thinking_binding_error(message: &str) -> bool {
|
||||
message.contains("bound to a different conversation")
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::base::ProviderDescriptor for DatabricksV2Provider {
|
||||
@@ -905,7 +963,7 @@ mod tests {
|
||||
mod gateway_path {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
use wiremock::matchers::{body_partial_json, method, path};
|
||||
use wiremock::matchers::{body_partial_json, header, method, path};
|
||||
use wiremock::{Mock, MockServer, ResponseTemplate};
|
||||
|
||||
fn provider(host: String) -> DatabricksV2Provider {
|
||||
@@ -1065,6 +1123,91 @@ mod tests {
|
||||
.expect("Claude model service should use the Anthropic Messages API");
|
||||
}
|
||||
|
||||
const BINDING_ERROR: &str = r#"{"error_code":"BAD_REQUEST","message":"{\"message\":\"messages.1.content.0: Invalid `signature` in `thinking` block. The block is bound to a different conversation. Remove the block, or set `thinking.block_binding.prefix_mismatch_behavior` to \\\"drop_block\\\". That setting requires the `thinking-binding-controls-2026-08-01` value in the `anthropic-beta` header. The `system` prompt differs from the one this block was created with.\"}"}"#;
|
||||
|
||||
fn has_block_binding(request: &wiremock::Request) -> bool {
|
||||
serde_json::from_slice::<Value>(&request.body)
|
||||
.unwrap()
|
||||
.pointer("/thinking/block_binding")
|
||||
.is_some()
|
||||
}
|
||||
|
||||
async fn complete_fable_5_2(provider: &DatabricksV2Provider) -> Result<(), ProviderError> {
|
||||
provider
|
||||
.complete(
|
||||
&ModelConfig::new("system.ai.claude-fable-5-2"),
|
||||
"system",
|
||||
&[Message::user().with_text("hi")],
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn claude_drops_thinking_bound_to_another_prefix_and_remembers_the_model() {
|
||||
let body = concat!(
|
||||
r#"data: {"type":"message_start","message":{"id":"msg_1","role":"assistant","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}"#,
|
||||
"\n",
|
||||
r#"data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}"#,
|
||||
"\n",
|
||||
r#"data: {"type":"message_stop"}"#,
|
||||
"\n",
|
||||
);
|
||||
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/ai-gateway/anthropic/v1/messages"))
|
||||
.and(|request: &wiremock::Request| !has_block_binding(request))
|
||||
.respond_with(ResponseTemplate::new(400).set_body_string(BINDING_ERROR))
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/ai-gateway/anthropic/v1/messages"))
|
||||
.and(header(
|
||||
"anthropic-beta",
|
||||
"thinking-binding-controls-2026-08-01",
|
||||
))
|
||||
.and(body_partial_json(json!({
|
||||
"thinking": {"block_binding": {"prefix_mismatch_behavior": "drop_block"}}
|
||||
})))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.set_body_string(body)
|
||||
.append_header("content-type", "text/event-stream"),
|
||||
)
|
||||
.expect(2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let provider = provider(server.uri());
|
||||
complete_fable_5_2(&provider)
|
||||
.await
|
||||
.expect("the binding error should be retried with drop_block");
|
||||
complete_fable_5_2(&provider)
|
||||
.await
|
||||
.expect("later requests should send drop_block up front");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn claude_does_not_retry_other_bad_requests() {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/ai-gateway/anthropic/v1/messages"))
|
||||
.respond_with(ResponseTemplate::new(400).set_body_string(
|
||||
r#"{"message":"messages.1.content.0: Invalid `signature` in `thinking` block."}"#,
|
||||
))
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let error = complete_fable_5_2(&provider(server.uri()))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(error, ProviderError::RequestFailed(_)), "{error}");
|
||||
}
|
||||
|
||||
#[test_case::test_case("catalog.schema.goose-glm-5-3" ; "glm 5.3")]
|
||||
#[test_case::test_case("catalog.schema.goose-kimi-k3" ; "kimi k3")]
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user