LLM output
This module defines data models that are standardized across different LLM providers.
docent._llm_util.data_models.llm_output
FinishReasonType
module-attribute
FinishReasonType = Literal['error', 'stop', 'length', 'tool_calls', 'content_filter', 'function_call', 'streaming', 'refusal']
Possible reasons for an LLM completion to finish.
LLMCompletion
Bases: BaseModel
A single completion from an LLM.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str | None
|
The generated text content. |
tool_calls |
list[ToolCall] | None
|
List of tool calls made during the completion. |
finish_reason |
FinishReasonType | None
|
Reason why the completion finished. |
top_logprobs |
list[list[TopLogprob]] | None
|
Probability distribution for top token choices. |
Source code in docent/_llm_util/data_models/llm_output.py
LLMOutput
dataclass
Container for LLM output, potentially with multiple completions.
Aggregates completions from an LLM along with metadata and error information.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
The name/identifier of the model used. |
completions |
list[LLMCompletion]
|
List of individual completions. |
errors |
list[LLMException]
|
List of error types encountered during generation. |
Source code in docent/_llm_util/data_models/llm_output.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
non_empty
property
Check if there are any completions.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if there's at least one completion, False otherwise. |
first
property
first: LLMCompletion | None
Get the first completion if available.
Returns:
| Type | Description |
|---|---|
LLMCompletion | None
|
LLMCompletion | None: The first completion or None if no completions exist. |
first_text
property
Get the text of the first completion if available.
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The text of the first completion or None if no completion exists. |
ToolCallPartial
dataclass
Partial representation of a tool call before full processing.
Used as an intermediate format before finalizing into a complete ToolCall.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str | None
|
The identifier for the tool call. |
required |
function
|
str | None
|
The name of the function to call. |
required |
arguments_raw
|
str | None
|
Raw JSON string of arguments for the function. |
required |
type
|
Literal['function']
|
The type of the tool call, always "function". |
required |
Source code in docent/_llm_util/data_models/llm_output.py
LLMCompletionPartial
Bases: LLMCompletion
Partial representation of an LLM completion before finalization.
Extends LLMCompletion but with tool_calls being a list of ToolCallPartial. This is used during the processing stage before tool calls are fully parsed.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_calls |
list[ToolCallPartial | None] | None
|
List of partial tool call representations. |
Source code in docent/_llm_util/data_models/llm_output.py
LLMOutputPartial
dataclass
Bases: LLMOutput
Partial representation of LLM output before finalization.
Extends LLMOutput but with completions being a list of LLMCompletionPartial. Used as an intermediate format during processing.
Attributes:
| Name | Type | Description |
|---|---|---|
completions |
list[LLMCompletionPartial]
|
List of partial completions. |
Source code in docent/_llm_util/data_models/llm_output.py
non_empty
property
Check if there are any completions.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if there's at least one completion, False otherwise. |
first
property
first: LLMCompletion | None
Get the first completion if available.
Returns:
| Type | Description |
|---|---|
LLMCompletion | None
|
LLMCompletion | None: The first completion or None if no completions exist. |
first_text
property
Get the text of the first completion if available.
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The text of the first completion or None if no completion exists. |
AsyncLLMOutputStreamingCallback
Bases: Protocol
Protocol for asynchronous streaming callbacks with batch index.
Defines the expected signature for callbacks that handle streaming output with a batch index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_index
|
The index of the current batch. |
required | |
llm_output
|
The LLM output for the current batch. |
required |
Source code in docent/_llm_util/data_models/llm_output.py
AsyncSingleLLMOutputStreamingCallback
Bases: Protocol
Protocol for asynchronous streaming callbacks without batch indexing.
Defines the expected signature for callbacks that handle streaming output without batch indexing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm_output
|
The LLM output to process. |
required |
Source code in docent/_llm_util/data_models/llm_output.py
AsyncEmbeddingStreamingCallback
Bases: Protocol
Protocol for sending progress updates for embedding generation.
Source code in docent/_llm_util/data_models/llm_output.py
finalize_llm_output_partial
finalize_llm_output_partial(partial: LLMOutputPartial) -> LLMOutput
Convert a partial LLM output into a finalized LLM output.
Processes tool calls by parsing their arguments from raw JSON strings, handles errors in JSON parsing, and provides warnings for truncated completions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partial
|
LLMOutputPartial
|
The partial LLM output to finalize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
LLMOutput |
LLMOutput
|
The finalized LLM output with processed tool calls. |
Raises:
| Type | Description |
|---|---|
CompletionTooLongException
|
If the completion was truncated due to length and resulted in empty text. |
ValueError
|
If tool call ID or function is missing in the partial data. |