github/copilot-sdk · error
JSON Schema serialization cannot fail
Error message
JSON Schema serialization cannot fail
What it means
schema_for<T> generates a JSON Schema via schemars and serializes it with .expect(), since schemars' generated schema types are always JSON-serializable by construction. A panic here means something in the toolchain broke that invariant — most plausibly NaN/Infinity defaults or a schemars/serde_json version mismatch.
Solutions
- Check for NaN/Infinity values returned by #[schemars(default)] or custom JsonSchema impls on T
- Align schemars and serde_json versions in Cargo.toml with what the SDK expects
- Replace custom JsonSchema impls with standard derives
- Bisect by calling schema_for on subfields of T to find the offending type
Example fix
// before
#[schemars(default = "nan_default")]
fn nan_default() -> f64 { f64::NAN } // breaks serialization invariant
// after
#[schemars(default = "zero_default")]
fn zero_default() -> f64 { 0.0 } Defensive patterns
Strategy: validation
Validate before calling
// Rust: before registering
fn has_json_safe_defaults<T: schemars::JsonSchema>() -> bool { /* audit #[schemars(default)] fns for NaN */ true } Prevention
- Avoid NaN/Infinity in #[schemars(default)] functions and custom JsonSchema impls
- Keep schemars/serde_json versions aligned with the SDK's expectations
- Prefer standard derive over hand-written JsonSchema impls
- Add a unit test calling schema_for::<T>() for every tool parameter type
When it happens
Trigger: Calling schema_for<T>() on a type whose generated schema contains non-serializable values (NaN/Infinity in default/minimum attributes) or with mismatched schemars/serde_json versions in the dependency graph.
Common situations: Custom JsonSchema impls emitting invalid values; cargo dependency resolution picking incompatible schemars 0.8 vs 1.x; user-written derive attributes like #[schemars(default = ...)] returning NaN.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- ExitPlanModeResult serialization cannot fail
- tool parameter schema must be a JSON object
- schema must be a valid JSON object string
- schema cannot be combined with defaultValue — express…
- Env is not supported with InProcessConnection: the…
AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09).
Data as JSON: /api/errors/8c08fb6992f53506.
Report an issue: GitHub.
Appendix: source
Thrown at rust/src/tool.rs:50
/// # Example
///
/// ```rust
/// use github_copilot_sdk::tool::{schema_for, JsonSchema};
///
/// #[derive(JsonSchema)]
/// struct Params {
/// /// City name
/// city: String,
/// }
///
/// let schema = schema_for::<Params>();
/// assert_eq!(schema["type"], "object");
/// assert!(schema["properties"]["city"].is_object());
/// ```
#[cfg(feature = "derive")]
pub fn schema_for<T: schemars::JsonSchema>() -> serde_json::Value {
let schema = schemars::schema_for!(T);
let mut value = serde_json::to_value(schema).expect("JSON Schema serialization cannot fail");
if let Some(obj) = value.as_object_mut() {
obj.remove("$schema");
obj.remove("title");
}
value
}
/// Convert a JSON Schema [`Value`](serde_json::Value) into the
/// [`Tool::parameters`](crate::types::Tool::parameters) map shape
/// expected by the protocol.
///
/// Panics if the input is not a JSON object — tool parameter schemas
/// are always top-level objects (`{"type": "object", ...}`). Pair with
/// `schema_for` (available with the `derive` feature) or a
/// `serde_json::json!(...)` literal.
///
/// Use [`try_tool_parameters`] when the schema comes from dynamic input and
/// should return a recoverable error instead of panicking.View on GitHub (pinned to cd8cf15dc3)