Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

DeepSeek Harness StreamChunk Protocol and Error Handling

DeepSeek Harness StreamChunk Protocol and Error Handling

What exactly does an adapter's stream() emit?

The answer is StreamChunk, a strictly ordered chunk protocol.

This article covers the chunk sequence, key rules and how to handle transmission errors.


StreamChunk Protocol

StreamChunk is the streaming protocol between Harness and adapters.

A content block begins with block-start, transfers incrementally via delta, and ends with block-end.

Text and tool calls are two different content block types, each going through start / delta / end.

When wrapping up all chunks, send usage to report token consumption first, then finish to declare the end reason.

StreamChunk fragmentation sequence diagram

The figure above shows a complete chunk sequence from top to bottom.

First a text block: block-start → text-delta × 2 → block-end.

Then a tool call block: block-start → tool-call-delta → block-end.

Finally usage and finish.

finish with reason stop means normal end; tool-calls means it requests tool execution.


Complete Chunk Sequence

The official docs provide exampleChunks, which emits all chunks of one generation in order.

text-delta can be split into multiple chunks and concatenated incrementally into the full text.

The argumentsDelta of tool-call-delta is an increment of the raw JSON text.

Example

// File path: sample code demonstrating a complete chunk sequence

import

{

CallId

,

type StreamChunk

}

from

'@deepseek-ai/dsh-llm'

async

function

exampleChunks

(

)

:

AsyncIterable

<

StreamChunk

{

// 1. Start each content block with block-start.

 

// Start a text block with index 0

  yield

{

type

:

'block-start'

,

index

:

0

,

blockType

:

'text'

}

 

// 2. Stream text through text-delta.

 

// Text delta, can be split into multiple chunks

  yield

{

type

:

'text-delta'

,

index

:

0

,

text

:

'runoob'

}

  yield

{

type

:

'text-delta'

,

index

:

0

,

text

:

' tutorial'

}

 

// 3. End each content block with block-end and the complete block.

 

// End with a complete block; index matches block-start

  yield

{

    type

:

'block-end'

,

    index

:

0

,

    block

:

{

type

:

'text'

,

text

:

'runoob tutorial'

}

,

 

}

 

// 4. Tool-call block.

 

// Start a tool call block with index 1

  yield

{

type

:

'block-start'

,

index

:

1

,

blockType

:

'tool-call'

}

 

// Tool name and argument delta; id generated by the CallId factory

  yield

{

    type

:

'tool-call-delta'

,

    index

:

1

,

    id

:

CallId

(

'call-123'

)

,

    name

:

'bash'

,

    argumentsDelta

:

'{"command":"echo runoob"}'

,

 

}

 

// End with a complete block; arguments is the assembled JSON text

  yield

{

    type

:

'block-end'

,

    index

:

1

,

    block

:

{

      type

:

'tool-call'

,

      id

:

CallId

(

'call-123'

)

,

      name

:

'bash'

,

      arguments

:

'{"command":"echo runoob"}'

,

   

}

,

 

}

 

// 5. Token usage.

 

// Report token usage; must be before finish

  yield

{

type

:

'usage'

,

usage

:

{

inputTokens

:

100

,

outputTokens

:

50

}

}

 

// 6. Finish reason.

 

// The final chunk, declaring the end reason

  yield

{

type

:

'finish'

,

reason

:

{

kind

:

'stop'

}

}

 

// Alternatively, { kind: 'tool-calls' } requests tool execution.

}

CallId is a built-in factory function of the protocol, used to generate tool call ids.

argumentsDelta can be produced completely in one chunk or incrementally across multiple chunks.


Key Rules

The protocol has five hard rules; violating any one will cause consumer parsing errors.

Rule Description
block-start and block-end come in pairs Every block-start must have a matching block-end
index starts at 0 and increases Used to identify the order of content blocks
argumentsDelta is a raw JSON increment Can be produced completely in one chunk or across multiple chunks
finish must be the last chunk No chunks may follow it
usage must come before finish Report token usage first, then declare the end

Both text-delta and tool-call-delta must carry the index of their owning block; do not interleave content blocks.


Error handling: expressing failures with LlmError

Adapters should throw transmission and protocol faults as LlmError with a stable code.

agent-loop retains the error and its code for diagnostics and policy handling.

Do not rely on plain Error being converted automatically.

A stable code lets upper layers match the error type precisely instead of parsing error strings.

Every provider HTTP request must also merge attributionHeaders().

Also pass options.signal to fetch so cancellation and resource release come to a complete stop.

Example

// File path: sample code, an HttpAdapter skeleton with error handling

import

{

  attributionHeaders

,

  LlmAdapter

,

  LlmError

,

  type GenerateOptions

,

  type StreamChunk

,

}

from

'@deepseek-ai/dsh-llm'

class

HttpAdapter

extends

LlmAdapter

{

 

// Constructor injects the endpoint address

  constructor

(

private readonly endpoint

:

string

)

{

   

super

(

)

 

}

  async

stream

(

options

:

GenerateOptions

)

:

AsyncIterable

<

StreamChunk

{

// Make the HTTP request, merge attribution headers, pass the abort signal

   

const

response

=

await fetch

(

this

.

endpoint

,

{

      method

:

'POST'

,

      headers

:

{

       

'content-type'

:

'application/json'

,

        ...

attributionHeaders

(

)

,

     

}

,

      body

:

JSON.

stringify

(

{

model

:

options.

model

,

messages

:

options.

messages

}

)

,

     

// When the caller requests cancellation, fetch aborts immediately

      ...

options

.

signal

?

{

signal

:

options.

signal

}

:

{

}

,

   

}

)

   

if

(

!

response.

ok

)

{

     

// Express transmission failure as LlmError with a stable code

     

throw

new

LlmError

(

`Provider API error

:

$

{

response.

status

}

`

,

'PROVIDER_HTTP_ERROR'

)

   

}

   

// A real adapter parses the response and emits the complete chunk sequence.

   

// A real adapter parses the response body here and produces the complete chunk sequence

    yield

{

type

:

'finish'

,

reason

:

{

kind

:

'stop'

}

}

 

}

}

attributionHeaders() merges attribution information into the request headers.

When options.signal exists it is passed as fetch's signal, stopping immediately on cancellation.

When response.ok is false, throw LlmError with code PROVIDER_HTTP_ERROR.

The skeleton only yields one finish; a real adapter parses the response body here.

The first argument of LlmError is the message, the second is the stable code.

Upper layers make policy decisions by code, so never change a code once published.


Fields That Must Not Be Silently Dropped

If GenerateOptions contains a field the adapter cannot support, throw LlmError rather than silently dropping it.

Reasoning metadata contains an ordered opaque ID, display name and optional configured default.

Preserve the authoritative option list given by the adapter, including the off returned by its upstream capability API.

Do not promote optional reasoning effort into a core enum, or adapters lose upstream flexibility.

The service validates the aggregated result and rejects explicitly specified but unsupported reasoning effort before calling stream().

Omitting reasoning means the model has no optional reasoning-effort capability.


Summary and self-test

In short: the StreamChunk protocol describes content with paired blocks and wraps up with usage and finish; failures are expressed with LlmError carrying a stable code.

Self-test 1: what is the minimum number of chunks for a text block?

Self-test 2: what is the order of usage and finish?

Self-test 3: why use LlmError with a code instead of plain Error when a request fails?