Skip to content

vllm.reasoning.granite_reasoning_parser

Classes:

GraniteReasoningParser

Bases: ReasoningParser

Reasoning parser for IBM Granite.

IBM granite models currently use "Here is my thought process:" and "Here is my response:" to separate its thinking / response outputs.

Methods:

_get_content_sections(current_text)

Parse the text to extract the reasoning content / content if we have them.

Parameters:

  • current_text

    (str) –

    The full previous + delta text.

Returns:

  • str | None –

    tuple[Optional[str], Optional[int], Optional[str]]: Tuple of len 3

  • int | None –

    containing the reasoning content, the length of the response seq

  • str | None –

    (if there is one) and the non-reasoning content.

_get_delta_message_with_both_bounds(delta_text, reasoning, response_content, current_text, response_seq_len)

Parse the delta message when the current text has both reasoning content and normal (response) content.

Parameters:

  • delta_text

    (str) –

    Text to consider and parse content from.

  • reasoning

    (str) –

    reasoning content from current_text.

  • response_content

    (str) –

    response content from current_text.

  • current_text

    (str) –

    The full previous + delta text.

  • response_seq_len

    (int) –

    Len of the complete response sequence used.

Returns:

  • DeltaMessage ( DeltaMessage ) –

    Message containing the parsed content.

_get_delta_message_with_no_reasoning_bounds(current_text, delta_text)

Parse the delta message when the current text has not yet completed its start of reasoning sequence.

Parameters:

  • current_text

    (str) –

    The full previous + delta text.

  • delta_text

    (str) –

    Text to consider and parse content from.

Returns:

  • DeltaMessage ( DeltaMessage ) –

    Message containing the parsed content.

_get_delta_message_with_no_response_bounds(current_text, reasoning, delta_text)

Parse the delta message when the current text has both reasoning content with no (response) content. NOTE that we may have overlapping tokens with the start of reasoning / start of response sequences on either side of the delta text.

Parameters:

  • current_text

    (str) –

    The full previous + delta text.

  • reasoning

    (str) –

    reasoning content from current_text.

  • delta_text

    (str) –

    Text to consider and parse content from.

Returns:

  • DeltaMessage ( DeltaMessage ) –

    Message containing the parsed content.

_is_reasoning_start_substr(text)

Check if a text matches one of the possible start reasoning seqs.

Parameters:

  • text

    (str) –

    Text to check for leading substr.

Returns:

  • bool ( bool ) –

    True if any of the possible reasoning start seqs match.

_is_response_start_substr(text)

Check if a text matches one of the possible start response seqs.

Parameters:

  • text

    (str) –

    Text to check for leading substr.

Returns:

  • bool ( bool ) –

    True if any of the possible response start seqs match.

extract_reasoning(model_output, request)

Extract the reasoning content & content sections, respectively. If the sequence doesn't match what we expect, i.e., the model generates something else, all content is considered non-reasoning content.

Parameters:

Returns:

  • str | None –

    tuple[Optional[str], Optional[str]]: Tuple pair containing the

  • str | None –

    reasoning content and non-reasoning content.

extract_reasoning_streaming(previous_text, current_text, delta_text, previous_token_ids, current_token_ids, delta_token_ids)

Extract the reasoning content / content emitted by granite models; If the sequence doesn't match what we expect, i.e., the model generates something else, all content is considered non-reasoning content.

NOTE: Granite models do not use a special token to start their reasoning and response sections; instead they have token sequences, e.g.,

    Here is my thought process: Foo Here is my response: Bar

This increases the complexity of correctly handling streams, since we need to watch for specific sequences and correctly parse them without dropping content that is potentially overlapping & spanning multiple delta messages.

Parameters:

  • previous_text

    (str) –

    Previous text outside of this delta message.

  • current_text

    (str) –

    Previous text + delta text.

  • delta_text

    (str) –

    Text to consider and parse content from.

  • previous_token_ids

    (Sequence[int]) –

    Token IDs of previous_text.

  • current_token_ids

    (Sequence[int]) –

    Token IDs of current_text.

  • delta_token_ids

    (Sequence[int]) –

    Token IDs of delta_text.

Returns:

  • DeltaMessage | None –

    Union[DeltaMessage, None] DeltaMessage with either reasoning content or content, or None.