跳转至

类型定义

本节记录了 ToolRegistry 库中使用的类型定义,用于与各种 LLM API 格式的兼容。

通用类型

库中通用的类型定义。

toolregistry.types

Type definitions for toolregistry.

ContentBlock module-attribute

ContentBlock = Union[TextBlock, ImageBlock]

Union type for all supported content block kinds.

Base64ImageSource

Bases: TypedDict

Base64-encoded image data with media type.

ChatCompetionMessageToolCallResult

Bases: BaseModel

Example usage

{ "role": "tool", "tool_call_id": tool_call.id, "content": str(result) }

content instance-attribute

content: str

Result of the tool call in string format.

role class-attribute instance-attribute

role: Literal['tool'] = 'tool'

The role of the message. Always tool.

tool_call_id instance-attribute

tool_call_id: str

The ID of the tool call that this message is responding to.

ChatCompletionMessage

Bases: BaseModel

content class-attribute instance-attribute

content: str | None = None

The contents of the message.

role class-attribute instance-attribute

role: Literal['assistant'] = 'assistant'

The role of the author of this message.

tool_calls class-attribute instance-attribute

tool_calls: list[ChatCompletionMessageToolCall] | None = None

The tool calls generated by the model, such as function calls or custom calls.

ChatCompletionMessageCustomToolCall

Bases: BaseModel

Custom tool call from OpenAI's updated SDK.

Example usage:

{ "id": "call_12345xyz", "type": "custom", "custom": { "name": "custom_tool", "input": "some input data" } }

custom instance-attribute

custom: Custom

The custom tool that the model called.

id instance-attribute

id: str

The ID of the tool call.

type class-attribute instance-attribute

type: Literal['custom'] = 'custom'

The type of the tool. Always custom.

ChatCompletionMessageFunctionToolCall

Bases: BaseModel

Function tool call from OpenAI's SDK.

Example usage:

{ "id": "call_12345xyz", "type": "function", "function": { "name": "get_weather", "arguments": "{"location":"Paris, France"}" } }

function instance-attribute

function: Function

The function that the model called.

id instance-attribute

id: str

The ID of the tool call.

type class-attribute instance-attribute

type: Literal['function'] = 'function'

The type of the tool. Always function.

Custom

Bases: BaseModel

Custom tool call parameters.

input instance-attribute

input: str

The input for the custom tool call generated by the model.

name instance-attribute

name: str

The name of the custom tool to call.

Function

Bases: BaseModel

arguments instance-attribute

arguments: str

The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.

name instance-attribute

name: str

The name of the function to call.

ImageBlock

Bases: TypedDict

An image content block with base64-encoded source.

ResponseFunctionToolCall

Bases: BaseModel

Example usage

{ "id": "fc_12345xyz", "call_id": "call_12345xyz", "type": "function_call", "name": "get_weather", "arguments": "{"location":"Paris, France"}" }

arguments instance-attribute

arguments: str

A JSON string of the arguments to pass to the function.

call_id instance-attribute

call_id: str

The unique ID of the function tool call generated by the model.

id class-attribute instance-attribute

id: str | None = None

The unique ID of the function tool call.

name instance-attribute

name: str

The name of the function to run.

status class-attribute instance-attribute

status: Literal['in_progress', 'completed', 'incomplete'] | None = None

The status of the item.

One of in_progress, completed, or incomplete. Populated when items are returned via API.

type class-attribute instance-attribute

type: Literal['function_call'] = 'function_call'

The type of the function tool call. Always function_call.

ResponseFunctionToolCallResult

Bases: BaseModel

Example usage

{ "type": "function_call_output", "call_id": tool_call.call_id, "output": str(result) }

call_id instance-attribute

call_id: str

The unique ID of the function tool call.

output instance-attribute

output: str

The output of the function tool call as a string.

type class-attribute instance-attribute

type: Literal['function_call_output'] = 'function_call_output'

The type of the function tool call result. Always function_call_output.

TextBlock

Bases: TypedDict

A plain text content block.

ToolCall

Bases: BaseModel

arguments instance-attribute

arguments: str

The arguments to call the function with, as generated by the model in JSON format.

id instance-attribute

id: str

The ID of the tool call.

name instance-attribute

name: str

The name of the function to call.

type class-attribute instance-attribute

type: Literal['function', 'custom'] = 'function'

The type of the tool call. Either 'function' or 'custom'.

from_tool_call classmethod

from_tool_call(tool_call: Any) -> ToolCall

Convert various tool call formats to ToolCall using Pydantic validation.

Supports the following formats: - ChatCompletionMessageFunctionToolCall (function calls) - ChatCompletionMessageCustomToolCall (custom calls) - ResponseFunctionToolCall (response API format) - Anthropic tool_use / server_tool_use blocks - Gemini functionCall parts - Dictionary representations of the above

Parameters:

Name Type Description Default
tool_call Any

Tool call object in various formats (Pydantic models or dicts)

required

Returns:

Name Type Description
ToolCall ToolCall

Normalized tool call object with proper type classification

Raises:

Type Description
TypeError

If the tool call format is not supported

ToolCallResult

Bases: BaseModel

id instance-attribute

id: str

The ID of the tool call.

result instance-attribute

result: Any

The result of the tool call.

convert_any_field_to_str

convert_any_field_to_str(value: Any) -> str

Convert result to string during serialization.

Parameters:

Name Type Description Default
value Any

Current value of any_field.

required

Returns:

Name Type Description
str str

String representation of the field.

build_assistant_message

build_assistant_message(tool_calls: list[ToolCall], *, api_format: API_FORMATS = 'openai-chat') -> list[dict[str, Any]]

Build the assistant message from tool calls in various API formats.

Reconstructs the assistant-side message containing tool call requests in the target API format. This function only handles the tool-call portion of the assistant message; content, thinking, reasoning, and other vendor-specific fields must be preserved by the caller from the original LLM response.

Parameters:

Name Type Description Default
tool_calls list[ToolCall]

A list of ToolCall objects (may contain mixed types).

required
api_format API_FORMATS

Target API format. Defaults to "openai-chat".

'openai-chat'

Returns:

Type Description
list[dict[str, Any]]

The assistant message as a list of dicts in the specified API format.

Raises:

Type Description
ValueError

If the API format is unsupported.

build_expanded_user_message

build_expanded_user_message(content_parts: list[dict[str, Any]], api_format: str) -> dict[str, Any]

Build a user message from expanded multimodal content parts.

Converts canonical content blocks (TextBlock / ImageBlock) into the user message format required by the target API.

Parameters:

Name Type Description Default
content_parts list[dict[str, Any]]

List of content block dicts produced by :func:expand_content_blocks.

required
api_format str

Target API format (e.g. "openai-chat", "anthropic", "gemini").

required

Returns:

Type Description
dict[str, Any]

A user message dict ready to append to the conversation.

build_tool_response

build_tool_response(tool_responses: dict[str, str | list], *, api_format: API_FORMATS = 'openai-chat', tool_calls: list[ToolCall] | None = None) -> list[dict[str, Any]]

Build tool result messages from tool execution responses.

Reconstructs the tool-result messages needed by the LLM to process function call outcomes. For Gemini format, tool_calls must be provided to resolve function names (Gemini uses function names instead of call IDs).

Values in tool_responses may be plain strings or list[ContentBlock] for multimodal results. All formats receive text-only tool results; multimodal content should be expanded into a separate user message upstream via :func:~toolregistry.types.content_blocks.expand_content_blocks.

Parameters:

Name Type Description Default
tool_responses dict[str, str | list]

Mapping of tool call IDs to results (str or list[ContentBlock]). Must preserve insertion order (guaranteed in Python 3.7+).

required
api_format API_FORMATS

Target API format. Defaults to "openai-chat".

'openai-chat'
tool_calls list[ToolCall] | None

Optional ToolCall list for Gemini name resolution. Falls back to call_id when unavailable.

None

Returns:

Type Description
list[dict[str, Any]]

Tool result messages in the specified API format.

Raises:

Type Description
ValueError

If the API format is unsupported.

content_blocks_to_text

content_blocks_to_text(blocks: list[ContentBlock]) -> str

Render content blocks as a plain-text string.

Text blocks are concatenated directly. Image blocks are replaced with a human-readable placeholder indicating the media type and approximate data size.

Parameters:

Name Type Description Default
blocks list[ContentBlock]

List of content blocks to render.

required

Returns:

Type Description
str

A plain-text representation of the content.

convert_tool_calls

convert_tool_calls(tool_calls: list[Any]) -> list[ToolCall]

Convert a list of tool calls into a list of ToolCall objects.

Parameters:

Name Type Description Default
tool_calls List[Any]

A list of tool call objects to convert.

required

Returns:

Type Description
list[ToolCall]

List[ToolCall]: A list of converted ToolCall objects.

expand_content_blocks

expand_content_blocks(tool_responses: dict[str, str | list]) -> tuple[dict[str, str | list], list[dict[str, Any]]]

Separate multimodal content from tool responses for uniform handling.

For each tool response that contains content blocks (multimodal), replaces the value with a placeholder string referencing a <tool-content> tag, and collects the actual content blocks into user message content parts that should follow the tool result messages.

This approach is provider-agnostic: all API formats receive text-only tool results, and multimodal content is delivered via a subsequent user message.

Parameters:

Name Type Description Default
tool_responses dict[str, str | list]

Mapping of tool call IDs to results. Values may be str or list[ContentBlock].

required

Returns:

Type Description
dict[str, str | list]

A tuple of:

list[dict[str, Any]]
  • text_only_responses -- Same mapping but with all content block lists replaced by placeholder strings.
tuple[dict[str, str | list], list[dict[str, Any]]]
  • extra_user_content -- List of content parts (dicts) for a user message. Empty if no multimodal content was found.

is_content_block_list

is_content_block_list(value: Any) -> bool

Check whether value is a list of content blocks.

A valid content block list is a non-empty list where every element is a dict with a "type" key whose value is one of the recognized content block types ("text" or "image").

Parameters:

Name Type Description Default
value Any

The value to check.

required

Returns:

Type Description
bool

True if value conforms to the content block list shape.

recover_assistant_message

recover_assistant_message(tool_calls: list[ToolCall], *, api_format: API_FORMATS = 'openai-chat') -> list[dict[str, Any]]

Deprecated: use :func:build_assistant_message instead.

recover_tool_message

recover_tool_message(tool_responses: dict[str, str | list], *, api_format: API_FORMATS = 'openai-chat', tool_calls: list[ToolCall] | None = None) -> list[dict[str, Any]]

Deprecated: use :func:build_tool_response instead.

toolregistry.types.common

ToolCall

Bases: BaseModel

arguments instance-attribute

arguments: str

The arguments to call the function with, as generated by the model in JSON format.

id instance-attribute

id: str

The ID of the tool call.

name instance-attribute

name: str

The name of the function to call.

type class-attribute instance-attribute

type: Literal['function', 'custom'] = 'function'

The type of the tool call. Either 'function' or 'custom'.

from_tool_call classmethod

from_tool_call(tool_call: Any) -> ToolCall

Convert various tool call formats to ToolCall using Pydantic validation.

Supports the following formats: - ChatCompletionMessageFunctionToolCall (function calls) - ChatCompletionMessageCustomToolCall (custom calls) - ResponseFunctionToolCall (response API format) - Anthropic tool_use / server_tool_use blocks - Gemini functionCall parts - Dictionary representations of the above

Parameters:

Name Type Description Default
tool_call Any

Tool call object in various formats (Pydantic models or dicts)

required

Returns:

Name Type Description
ToolCall ToolCall

Normalized tool call object with proper type classification

Raises:

Type Description
TypeError

If the tool call format is not supported

ToolCallResult

Bases: BaseModel

id instance-attribute

id: str

The ID of the tool call.

result instance-attribute

result: Any

The result of the tool call.

convert_any_field_to_str

convert_any_field_to_str(value: Any) -> str

Convert result to string during serialization.

Parameters:

Name Type Description Default
value Any

Current value of any_field.

required

Returns:

Name Type Description
str str

String representation of the field.

build_assistant_message

build_assistant_message(tool_calls: list[ToolCall], *, api_format: API_FORMATS = 'openai-chat') -> list[dict[str, Any]]

Build the assistant message from tool calls in various API formats.

Reconstructs the assistant-side message containing tool call requests in the target API format. This function only handles the tool-call portion of the assistant message; content, thinking, reasoning, and other vendor-specific fields must be preserved by the caller from the original LLM response.

Parameters:

Name Type Description Default
tool_calls list[ToolCall]

A list of ToolCall objects (may contain mixed types).

required
api_format API_FORMATS

Target API format. Defaults to "openai-chat".

'openai-chat'

Returns:

Type Description
list[dict[str, Any]]

The assistant message as a list of dicts in the specified API format.

Raises:

Type Description
ValueError

If the API format is unsupported.

build_tool_response

build_tool_response(tool_responses: dict[str, str | list], *, api_format: API_FORMATS = 'openai-chat', tool_calls: list[ToolCall] | None = None) -> list[dict[str, Any]]

Build tool result messages from tool execution responses.

Reconstructs the tool-result messages needed by the LLM to process function call outcomes. For Gemini format, tool_calls must be provided to resolve function names (Gemini uses function names instead of call IDs).

Values in tool_responses may be plain strings or list[ContentBlock] for multimodal results. All formats receive text-only tool results; multimodal content should be expanded into a separate user message upstream via :func:~toolregistry.types.content_blocks.expand_content_blocks.

Parameters:

Name Type Description Default
tool_responses dict[str, str | list]

Mapping of tool call IDs to results (str or list[ContentBlock]). Must preserve insertion order (guaranteed in Python 3.7+).

required
api_format API_FORMATS

Target API format. Defaults to "openai-chat".

'openai-chat'
tool_calls list[ToolCall] | None

Optional ToolCall list for Gemini name resolution. Falls back to call_id when unavailable.

None

Returns:

Type Description
list[dict[str, Any]]

Tool result messages in the specified API format.

Raises:

Type Description
ValueError

If the API format is unsupported.

convert_tool_calls

convert_tool_calls(tool_calls: list[Any]) -> list[ToolCall]

Convert a list of tool calls into a list of ToolCall objects.

Parameters:

Name Type Description Default
tool_calls List[Any]

A list of tool call objects to convert.

required

Returns:

Type Description
list[ToolCall]

List[ToolCall]: A list of converted ToolCall objects.

recover_assistant_message

recover_assistant_message(tool_calls: list[ToolCall], *, api_format: API_FORMATS = 'openai-chat') -> list[dict[str, Any]]

Deprecated: use :func:build_assistant_message instead.

recover_tool_message

recover_tool_message(tool_responses: dict[str, str | list], *, api_format: API_FORMATS = 'openai-chat', tool_calls: list[ToolCall] | None = None) -> list[dict[str, Any]]

Deprecated: use :func:build_tool_response instead.

OpenAI 类型

用于 OpenAI API 兼容的类型定义。

toolregistry.types.openai

OpenAI API types for toolregistry.

ChatCompetionMessageToolCallResult

Bases: BaseModel

Example usage

{ "role": "tool", "tool_call_id": tool_call.id, "content": str(result) }

content instance-attribute

content: str

Result of the tool call in string format.

role class-attribute instance-attribute

role: Literal['tool'] = 'tool'

The role of the message. Always tool.

tool_call_id instance-attribute

tool_call_id: str

The ID of the tool call that this message is responding to.

ChatCompletionMessage

Bases: BaseModel

content class-attribute instance-attribute

content: str | None = None

The contents of the message.

role class-attribute instance-attribute

role: Literal['assistant'] = 'assistant'

The role of the author of this message.

tool_calls class-attribute instance-attribute

tool_calls: list[ChatCompletionMessageToolCall] | None = None

The tool calls generated by the model, such as function calls or custom calls.

ChatCompletionMessageCustomToolCall

Bases: BaseModel

Custom tool call from OpenAI's updated SDK.

Example usage:

{ "id": "call_12345xyz", "type": "custom", "custom": { "name": "custom_tool", "input": "some input data" } }

custom instance-attribute

custom: Custom

The custom tool that the model called.

id instance-attribute

id: str

The ID of the tool call.

type class-attribute instance-attribute

type: Literal['custom'] = 'custom'

The type of the tool. Always custom.

ChatCompletionMessageFunctionToolCall

Bases: BaseModel

Function tool call from OpenAI's SDK.

Example usage:

{ "id": "call_12345xyz", "type": "function", "function": { "name": "get_weather", "arguments": "{"location":"Paris, France"}" } }

function instance-attribute

function: Function

The function that the model called.

id instance-attribute

id: str

The ID of the tool call.

type class-attribute instance-attribute

type: Literal['function'] = 'function'

The type of the tool. Always function.

Custom

Bases: BaseModel

Custom tool call parameters.

input instance-attribute

input: str

The input for the custom tool call generated by the model.

name instance-attribute

name: str

The name of the custom tool to call.

Function

Bases: BaseModel

arguments instance-attribute

arguments: str

The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.

name instance-attribute

name: str

The name of the function to call.

ResponseFunctionToolCall

Bases: BaseModel

Example usage

{ "id": "fc_12345xyz", "call_id": "call_12345xyz", "type": "function_call", "name": "get_weather", "arguments": "{"location":"Paris, France"}" }

arguments instance-attribute

arguments: str

A JSON string of the arguments to pass to the function.

call_id instance-attribute

call_id: str

The unique ID of the function tool call generated by the model.

id class-attribute instance-attribute

id: str | None = None

The unique ID of the function tool call.

name instance-attribute

name: str

The name of the function to run.

status class-attribute instance-attribute

status: Literal['in_progress', 'completed', 'incomplete'] | None = None

The status of the item.

One of in_progress, completed, or incomplete. Populated when items are returned via API.

type class-attribute instance-attribute

type: Literal['function_call'] = 'function_call'

The type of the function tool call. Always function_call.

ResponseFunctionToolCallResult

Bases: BaseModel

Example usage

{ "type": "function_call_output", "call_id": tool_call.call_id, "output": str(result) }

call_id instance-attribute

call_id: str

The unique ID of the function tool call.

output instance-attribute

output: str

The output of the function tool call as a string.

type class-attribute instance-attribute

type: Literal['function_call_output'] = 'function_call_output'

The type of the function tool call result. Always function_call_output.

toolregistry.types.openai.chat_completion

ChatCompetionMessageToolCallResult

Bases: BaseModel

Example usage

{ "role": "tool", "tool_call_id": tool_call.id, "content": str(result) }

content instance-attribute

content: str

Result of the tool call in string format.

role class-attribute instance-attribute

role: Literal['tool'] = 'tool'

The role of the message. Always tool.

tool_call_id instance-attribute

tool_call_id: str

The ID of the tool call that this message is responding to.

ChatCompletionMessage

Bases: BaseModel

content class-attribute instance-attribute

content: str | None = None

The contents of the message.

role class-attribute instance-attribute

role: Literal['assistant'] = 'assistant'

The role of the author of this message.

tool_calls class-attribute instance-attribute

tool_calls: list[ChatCompletionMessageToolCall] | None = None

The tool calls generated by the model, such as function calls or custom calls.

ChatCompletionMessageCustomToolCall

Bases: BaseModel

Custom tool call from OpenAI's updated SDK.

Example usage:

{ "id": "call_12345xyz", "type": "custom", "custom": { "name": "custom_tool", "input": "some input data" } }

custom instance-attribute

custom: Custom

The custom tool that the model called.

id instance-attribute

id: str

The ID of the tool call.

type class-attribute instance-attribute

type: Literal['custom'] = 'custom'

The type of the tool. Always custom.

ChatCompletionMessageFunctionToolCall

Bases: BaseModel

Function tool call from OpenAI's SDK.

Example usage:

{ "id": "call_12345xyz", "type": "function", "function": { "name": "get_weather", "arguments": "{"location":"Paris, France"}" } }

function instance-attribute

function: Function

The function that the model called.

id instance-attribute

id: str

The ID of the tool call.

type class-attribute instance-attribute

type: Literal['function'] = 'function'

The type of the tool. Always function.

Custom

Bases: BaseModel

Custom tool call parameters.

input instance-attribute

input: str

The input for the custom tool call generated by the model.

name instance-attribute

name: str

The name of the custom tool to call.

Function

Bases: BaseModel

arguments instance-attribute

arguments: str

The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.

name instance-attribute

name: str

The name of the function to call.

toolregistry.types.openai.response

ResponseFunctionToolCall

Bases: BaseModel

Example usage

{ "id": "fc_12345xyz", "call_id": "call_12345xyz", "type": "function_call", "name": "get_weather", "arguments": "{"location":"Paris, France"}" }

arguments instance-attribute

arguments: str

A JSON string of the arguments to pass to the function.

call_id instance-attribute

call_id: str

The unique ID of the function tool call generated by the model.

id class-attribute instance-attribute

id: str | None = None

The unique ID of the function tool call.

name instance-attribute

name: str

The name of the function to run.

status class-attribute instance-attribute

status: Literal['in_progress', 'completed', 'incomplete'] | None = None

The status of the item.

One of in_progress, completed, or incomplete. Populated when items are returned via API.

type class-attribute instance-attribute

type: Literal['function_call'] = 'function_call'

The type of the function tool call. Always function_call.

ResponseFunctionToolCallResult

Bases: BaseModel

Example usage

{ "type": "function_call_output", "call_id": tool_call.call_id, "output": str(result) }

call_id instance-attribute

call_id: str

The unique ID of the function tool call.

output instance-attribute

output: str

The output of the function tool call as a string.

type class-attribute instance-attribute

type: Literal['function_call_output'] = 'function_call_output'

The type of the function tool call result. Always function_call_output.

Anthropic 类型

用于 Anthropic API 兼容的类型定义。

toolregistry.types.anthropic

Anthropic API types for toolregistry.

Format conversion for Anthropic tool schemas, tool calls, and tool results is handled via llm-rosetta (pip install toolregistry[rosetta]). No Pydantic models are defined here as llm-rosetta operates on plain dicts.

Gemini 类型

用于 Google Gemini API 兼容的类型定义。

toolregistry.types.gemini

Gemini API types for toolregistry.

Format conversion for Gemini tool schemas, tool calls, and tool results is handled via llm-rosetta (pip install toolregistry[rosetta]). No Pydantic models are defined here as llm-rosetta operates on plain dicts.