nemo_gym.token_id_capture.fingerprint

View as Markdown

Fingerprint conversation content across serving dialects.

These are pure functions over message lists. They hold no state and read no store. Any server may import them. Lineage state stays confined to the capture locus and the finalizer.

assistant_fingerprint hashes only model-authored turns. It identifies the call that produced the last model-authored turn. conversation_digest hashes every turn, including tool results. A parent resolver uses it to verify context before reusing tokens.

Chat, Responses, and Anthropic shapes normalize to the same hash input. The hash layout is tagged and length-delimited. No concatenation of fields can collide with another field boundary.

Module Contents

Functions

NameDescription
_canonical_jsonSerialize JSON-compatible prompt content without losing structure.
_content_ofReturn typed content parts without discarding prompt-shaping blocks.
_is_assistant_authoredReturn whether the model produced this item.
_tool_results_ofReturn tool result identities and payloads across dialects.
_tools_ofReturn tool calls as (id, name, canonical arguments) tuples.
_update_fieldHash one tagged, length-delimited UTF-8 field.
assistant_fingerprintFingerprint the model-authored turns of a request, in order.
canonicalize_tool_argumentsNormalize a tool call’s arguments for comparison only.
conversation_digestHash every turn of a conversation, model-authored or not.

Data

FINGERPRINT_VERSION

_CONTEXT_DOMAIN

_FINGERPRINT_DOMAIN

API

nemo_gym.token_id_capture.fingerprint._canonical_json(
value: typing.Any
) -> str

Serialize JSON-compatible prompt content without losing structure.

nemo_gym.token_id_capture.fingerprint._content_of(
content: typing.Any
) -> list[tuple[str, str]]

Return typed content parts without discarding prompt-shaping blocks.

Tool calls are normalized separately by _tools_of. Tool results are normalized separately by _tool_results_of.

nemo_gym.token_id_capture.fingerprint._is_assistant_authored(
message: dict
) -> bool

Return whether the model produced this item.

Chat and Anthropic use the assistant role. Responses tool calls are roleless function_call items.

nemo_gym.token_id_capture.fingerprint._tool_results_of(
message: dict
) -> list[tuple[str, str]]

Return tool result identities and payloads across dialects.

Responses stores results in standalone function_call_output items. Anthropic stores results in tool_result content blocks. Chat stores results as plain message content.

nemo_gym.token_id_capture.fingerprint._tools_of(
message: dict
) -> list[tuple[str, str, str]]

Return tool calls as (id, name, canonical arguments) tuples.

Chat stores calls in the message’s tool_calls field. Anthropic stores calls in tool_use content blocks. Responses stores each call as a standalone function_call item.

nemo_gym.token_id_capture.fingerprint._update_field(
hasher: typing.Any,
tag: bytes,
value: str
) -> None

Hash one tagged, length-delimited UTF-8 field.

nemo_gym.token_id_capture.fingerprint.assistant_fingerprint(
messages: list[dict]
) -> str

Fingerprint the model-authored turns of a request, in order.

The fingerprint identifies the call that produced the last model-authored turn. User and tool content is excluded from the lookup key. Dialect-specific tool-call shapes normalize to the same hash input.

nemo_gym.token_id_capture.fingerprint.canonicalize_tool_arguments(
value: typing.Any
) -> str

Normalize a tool call’s arguments for comparison only.

Harnesses can reserialize tool-call arguments between turns. Comparison uses sorted-key JSON with normalized separators. The record retains the model’s original string.

nemo_gym.token_id_capture.fingerprint.conversation_digest(
messages: list[dict]
) -> str

Hash every turn of a conversation, model-authored or not.

assistant_fingerprint ignores user and tool content. This digest covers that omitted context. A mismatch rejects the parent before its tokens are reused.

nemo_gym.token_id_capture.fingerprint.FINGERPRINT_VERSION = 1
nemo_gym.token_id_capture.fingerprint._CONTEXT_DOMAIN = b'nemo-gym-lineage-context'
nemo_gym.token_id_capture.fingerprint._FINGERPRINT_DOMAIN = b'nemo-gym-lineage'