github/copilot-sdk · error

failed to generate schema for type

Error message

failed to generate schema for type %v: %v

What it means

generateSchemaForType uses google/jsonschema-go (jsonschema.ForType) to derive a JSON schema from a Go type passed to DefineTool. If ForType cannot build a schema for the (dereferenced) reflect.Type, the function panics with the type and underlying error. DefineTool requires every parameter type to be representable as a JSON schema.

Solutions

  1. Simplify the tool input type to JSON-representable fields (strings, numbers, bools, slices, maps, nested structs)
  2. Remove unsupported field types (func, chan, unsafe pointers) or wrap them behind a supported representation
  3. Check the wrapped error for the exact field/type jsonschema.ForType rejected
  4. Dereference expectations: pass the concrete value type rather than exotic pointer/channel types

Example fix

// before
type In struct { Callback func(string) `json:"cb"` }
DefineTool("t", "d", In{})
// after
type In struct { CallbackName string `json:"callbackName"` }
DefineTool("t", "d", In{})
Defensive patterns

Strategy: validation

Validate before calling

func jsonSchemaSafe(t reflect.Type) error {
	for t.Kind() == reflect.Pointer { t = t.Elem() }
	_, err := jsonschema.ForType(t, nil)
	return err
}

Type guard

func isJSONSchemaSafeKind(k reflect.Kind) bool {
	switch k {
	case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array,
		reflect.String, reflect.Bool,
		reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Float32, reflect.Float64, reflect.Interface:
		return true
	}
	return false
}

Prevention

When it happens

Trigger: Calling DefineTool with a parameter struct/type that jsonschema.ForType cannot handle — e.g. types containing unsupported fields (funcs, chans, unexported-only fields, unsupported map key types) or a nil/invalid reflect type reached after pointer dereferencing (t.Elem()).

Common situations: Defining a tool whose input struct includes channels, function fields, complex numbers, or custom types without JSON-schema support; upgrading the jsonschema-go library changes what is supported; accidentally passing a non-struct such as a func type.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/a08f20a2d7c23197. Report an issue: GitHub.

Appendix: source

Thrown at go/definetool.go:217

	return tr, true
}

// generateSchemaForType generates a JSON schema map from a Go type using reflection.
// Panics if schema generation fails, as this indicates a programming error.
func generateSchemaForType(t reflect.Type) map[string]any {
	if t == nil {
		return nil
	}

	// Handle pointer types
	if t.Kind() == reflect.Pointer {
		t = t.Elem()
	}

	// Use google/jsonschema-go to generate the schema
	schema, err := jsonschema.ForType(t, nil)
	if err != nil {
		panic(fmt.Sprintf("failed to generate schema for type %v: %v", t, err))
	}

	// Convert schema to map[string]any
	schemaBytes, err := json.Marshal(schema)
	if err != nil {
		panic(fmt.Sprintf("failed to marshal schema for type %v: %v", t, err))
	}

	var schemaMap map[string]any
	if err := json.Unmarshal(schemaBytes, &schemaMap); err != nil {
		panic(fmt.Sprintf("failed to unmarshal schema for type %v: %v", t, err))
	}

	return schemaMap
}

View on GitHub (pinned to cd8cf15dc3)