@@ -851,6 +851,50 @@ describe('output-format-handlers', () => {
851851 expect ( objectResultChunk ) . toBeDefined ( ) ;
852852 expect ( objectResultChunk ?. object ) . toEqual ( { title : 'Test' , count : 5 } ) ;
853853 } ) ;
854+
855+ it ( 'should preserve ```json fences inside valid JSON string values' , async ( ) => {
856+ const schema = z . object ( {
857+ response : z . string ( ) ,
858+ status : z . string ( ) ,
859+ } ) ;
860+
861+ const transformer = createObjectStreamTransformer ( {
862+ structuredOutput : { schema } ,
863+ } ) ;
864+
865+ const response = 'API example:\n```json\nPOST /v1/payments\n{\n "customerId": "cust_123"\n}\n```' ;
866+
867+ const streamParts : ChunkType < typeof schema > [ ] = [
868+ {
869+ type : 'text-delta' ,
870+ runId : 'test-run' ,
871+ from : ChunkFrom . AGENT ,
872+ payload : {
873+ id : '1' ,
874+ text : JSON . stringify ( { response, status : 'ok' } ) ,
875+ } ,
876+ } ,
877+ {
878+ type : 'finish' ,
879+ runId : 'test-run' ,
880+ from : ChunkFrom . AGENT ,
881+ payload : {
882+ stepResult : { reason : 'stop' } ,
883+ output : { usage : { inputTokens : 0 , outputTokens : 0 , totalTokens : 0 } } ,
884+ metadata : { } ,
885+ messages : { all : [ ] , user : [ ] , nonUser : [ ] } ,
886+ } ,
887+ } ,
888+ ] ;
889+
890+ // @ts -expect-error - web/stream readable stream type error
891+ const stream = convertArrayToReadableStream ( streamParts ) . pipeThrough ( transformer ) ;
892+ const chunks = await convertAsyncIterableToArray ( stream ) ;
893+
894+ const objectResultChunk = chunks . find ( c => c ?. type === 'object-result' ) ;
895+ expect ( objectResultChunk ) . toBeDefined ( ) ;
896+ expect ( objectResultChunk ?. object ) . toEqual ( { response, status : 'ok' } ) ;
897+ } ) ;
854898 } ) ;
855899
856900 describe ( 'unescaped newlines in JSON strings' , ( ) => {
0 commit comments