Use correct schema for client sampling validation when tools are present (#1347)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Paul Carleton <paulcarletonjr@gmail.com>
This commit is contained in:
Ola Hungerford 2026-01-19 09:40:59 -07:00 • committed by GitHub
commit 2edbe8c82e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -43,6 +43,7 @@ import {
CompleteResultSchema,
CreateMessageRequestSchema,
CreateMessageResultSchema,
CreateMessageResultWithToolsSchema,
CreateTaskResultSchema,
ElicitRequestSchema,
ElicitResultSchema,
@ -458,8 +459,10 @@ export class Client<
return taskValidationResult.data;
}
// For non-task requests, validate against CreateMessageResultSchema
const validationResult = safeParse(CreateMessageResultSchema, result);
// For non-task requests, validate against appropriate schema based on tools presence
const hasTools = params.tools || params.toolChoice;
const resultSchema = hasTools ? CreateMessageResultWithToolsSchema : CreateMessageResultSchema;
const validationResult = safeParse(resultSchema, result);
if (!validationResult.success) {
const errorMessage =
validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);