{ "cells": [ { "cell_type": "markdown", "id": "05733998", "metadata": {}, "source": [ "# GenAI messages Python models\n", "\n", "This file defines Python models for system instructions, input, and output messages.\n", "These models are provided for reference only.\n", "\n", "The model definitions are based on [Pydantic](https://github.com/pydantic/pydantic)." ] }, { "cell_type": "code", "execution_count": null, "id": "3c4ec73b", "metadata": { "vscode": { "languageId": "shellscript" } }, "outputs": [], "source": [ "pip install pydantic~=2.0" ] }, { "cell_type": "markdown", "id": "b335c0ec", "metadata": { "vscode": { "languageId": "shellscript" } }, "source": [ "## Common Code" ] }, { "cell_type": "code", "execution_count": 1, "id": "5124fe15", "metadata": {}, "outputs": [], "source": [ "from enum import Enum\n", "import json\n", "from typing import Any, List, Literal, Optional, Union\n", "from pydantic import BaseModel, Field, RootModel\n", "\n", "class TextPart(BaseModel):\n", " \"\"\"\n", " Represents text content sent to or received from the model.\n", " \"\"\"\n", " type: Literal['text'] = Field(description=\"The type of the content captured in this part.\")\n", " content: str = Field(description=\"Text content sent to or received from the model.\")\n", "\n", " class Config:\n", " extra = \"allow\"\n", "\n", "class ToolCallRequestPart(BaseModel):\n", " \"\"\"\n", " Represents a tool call requested by the model.\n", " \"\"\"\n", " type: Literal[\"tool_call\"] = Field(description=\"The type of the content captured in this part.\")\n", " id: Optional[str] = Field(default=None, description=\"Unique identifier for the tool call.\")\n", " name: str = Field(description=\"Name of the tool.\")\n", " arguments: Any = Field(None, description=\"Arguments for the tool call.\")\n", "\n", " class Config:\n", " extra = \"allow\"\n", "\n", "class ToolCallResponsePart(BaseModel):\n", " \"\"\"\n", " Represents a tool call result sent to the model or a built-in tool call outcome and details.\n", " \"\"\"\n", " type: Literal['tool_call_response'] = Field(description=\"The type of the content captured in this part.\")\n", " id: Optional[str] = Field(default=None, description=\"Unique tool call identifier.\")\n", " response: Any = Field(description=\"Tool call response.\")\n", "\n", " class Config:\n", " extra = \"allow\"\n", "\n", "class GenericPart(BaseModel):\n", " \"\"\"\n", " Represents an arbitrary message part with any type and properties.\n", " This allows for extensibility with custom message part types.\n", " \"\"\"\n", " type: str = Field(description=\"The type of the content captured in this part.\")\n", "\n", " class Config:\n", " extra = \"allow\"\n", "\n", "# This Union without discriminator will generate anyOf in JSON schema\n", "MessagePart = Union[\n", " TextPart,\n", " ToolCallRequestPart,\n", " ToolCallResponsePart,\n", " GenericPart, # Catch-all for any other type\n", " # Add other message part types here as needed,\n", " # e.g. image URL, image blob, audio URL, structured output, hosted tool call, etc.\n", "]\n", "\n", "class Role(str, Enum):\n", " SYSTEM = \"system\"\n", " USER = \"user\"\n", " ASSISTANT = \"assistant\"\n", " TOOL = \"tool\"\n", "\n", "class ChatMessage(BaseModel):\n", " role: Union[Role, str] = Field(\n", " description=\"Role of the entity that created the message.\")\n", " parts: List[MessagePart] = Field(\n", " description=\"List of message parts that make up the message content.\")\n", "\n", " class Config:\n", " extra = \"allow\"" ] }, { "cell_type": "markdown", "id": "bd7b678f", "metadata": {}, "source": [ "\n", "## `gen_ai.input.messages` model\n", "\n", "Corresponding attribute: [`gen_ai.input.messages`](/docs/registry/attributes/gen-ai.md#gen-ai-input-messages).\n", "JSON schema: [`gen_ai.input.messages.json`](../gen-ai-input-messages.json)" ] }, { "cell_type": "code", "execution_count": null, "id": "0133d3a6", "metadata": {}, "outputs": [], "source": [ "class InputMessages(RootModel[List[ChatMessage]]):\n", " \"\"\"\n", " Represents the list of input messages sent to the model.\n", " \"\"\"\n", " pass\n", "\n", "# Print the JSON schema for the InputMessages model\n", "print(json.dumps(InputMessages.model_json_schema(), indent=4))" ] }, { "cell_type": "markdown", "id": "033e5960", "metadata": {}, "source": [ "## `gen_ai.output.messages` model\n", "\n", "Corresponding attribute: [`gen_ai.output.messages`](/docs/registry/attributes/gen-ai.md#gen-ai-output-messages).\n", "JSON schema: [`gen_ai-output-messages.json`](../gen-ai-output-messages.json)" ] }, { "cell_type": "code", "execution_count": null, "id": "12a12112", "metadata": {}, "outputs": [], "source": [ "class FinishReason(str, Enum):\n", " \"\"\"\n", " Represents the reason for finishing the generation.\n", " \"\"\"\n", "\n", " STOP = \"stop\"\n", " LENGTH = \"length\"\n", " CONTENT_FILTER = \"content_filter\"\n", " TOOL_CALL = \"tool_call\"\n", " ERROR = \"error\"\n", "\n", "class OutputMessage(ChatMessage):\n", " \"\"\"\n", " Represents an output message generated by the model or agent. The output message captures\n", " specific response (choice, candidate).\n", " \"\"\"\n", " finish_reason: Union[FinishReason, str] = Field(description=\"Reason for finishing the generation.\")\n", "\n", "class OutputMessages(RootModel[List[OutputMessage]]):\n", " \"\"\"\n", " Represents the list of output messages generated by the model or agent.\n", " \"\"\"\n", " pass\n", "\n", "# Print the JSON schema for the OutputMessages model\n", "print(json.dumps(OutputMessages.model_json_schema(), indent=4))" ] }, { "cell_type": "markdown", "id": "5c035888", "metadata": {}, "source": [ "## `gen_ai.system_instructions` model\n", "\n", "Corresponding attribute: [`gen_ai.system_instructions`](/docs/registry/attributes/gen-ai.md#gen-ai-system-instructions).\n", "JSON schema: [`gen_ai-system-instructions.json`](../gen-ai-system-instructions.json)" ] }, { "cell_type": "code", "execution_count": null, "id": "faa33b95", "metadata": {}, "outputs": [], "source": [ "class SystemInstructions(RootModel[List[MessagePart]]):\n", " \"\"\"\n", " Represents the list of input messages sent to the model.\n", " \"\"\"\n", " pass\n", "\n", "# Print the JSON schema for the SystemInstructions model\n", "print(json.dumps(SystemInstructions.model_json_schema(), indent=4))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }