@@ -173,6 +173,44 @@ function getApprovalIdentity(approval: ApprovalItemLike): string | undefined {
173173 }
174174}
175175
176+ function formatFinalOutputTypeError ( error : unknown ) : string {
177+ // Surface structured output validation hints without echoing potentially large or sensitive payloads.
178+ try {
179+ if ( error instanceof z . ZodError ) {
180+ const issue = error . issues [ 0 ] ;
181+ if ( issue ) {
182+ const issuePathParts = Array . isArray ( issue . path ) ? issue . path : [ ] ;
183+ const issuePath =
184+ issuePathParts . length > 0
185+ ? issuePathParts . map ( ( part ) => String ( part ) ) . join ( '.' )
186+ : '(root)' ;
187+ const message = truncateForDeveloper ( issue . message ?? '' ) ;
188+ return `Invalid output type: final assistant output failed schema validation at "${ issuePath } " (${ message } ).` ;
189+ }
190+ return 'Invalid output type: final assistant output failed schema validation.' ;
191+ }
192+
193+ if ( error instanceof Error && error . message ) {
194+ return `Invalid output type: ${ truncateForDeveloper ( error . message ) } ` ;
195+ }
196+ } catch {
197+ // Swallow formatting errors so we can return a generic message below.
198+ }
199+
200+ return 'Invalid output type: final assistant output did not match the expected schema.' ;
201+ }
202+
203+ function truncateForDeveloper ( message : string , maxLength = 160 ) : string {
204+ const trimmed = message . trim ( ) ;
205+ if ( ! trimmed ) {
206+ return 'Schema validation failed.' ;
207+ }
208+ if ( trimmed . length <= maxLength ) {
209+ return trimmed ;
210+ }
211+ return `${ trimmed . slice ( 0 , maxLength - 3 ) } ...` ;
212+ }
213+
176214/**
177215 * @internal
178216 * Walks a raw model response and classifies each item so the runner can schedule follow-up work.
@@ -898,13 +936,14 @@ export async function resolveTurnAfterModelResponse<TContext>(
898936 ) ;
899937 const [ error ] = await safeExecute ( ( ) => parser ( potentialFinalOutput ) ) ;
900938 if ( error ) {
939+ const outputErrorMessage = formatFinalOutputTypeError ( error ) ;
901940 addErrorToCurrentSpan ( {
902- message : 'Invalid output type' ,
941+ message : outputErrorMessage ,
903942 data : {
904943 error : String ( error ) ,
905944 } ,
906945 } ) ;
907- throw new ModelBehaviorError ( 'Invalid output type' ) ;
946+ throw new ModelBehaviorError ( outputErrorMessage ) ;
908947 }
909948
910949 return new SingleStepResult (
0 commit comments