Skip to content

datarobot_genai.drmcpbase.dynamic_tools.utils

utils

format_response_as_tool_result

format_response_as_tool_result(data: bytes, content_type: str, charset: str) -> ToolResult

Format the deployment response into a ToolResult.

Using structured_content, to return as much information about the response as possible, for LLMs to correctly interpret the response.

Source code in datarobot_genai/drmcpbase/dynamic_tools/utils.py
def format_response_as_tool_result(data: bytes, content_type: str, charset: str) -> ToolResult:
    """Format the deployment response into a ToolResult.

    Using structured_content, to return as much information about
    the response as possible, for LLMs to correctly interpret the
    response.
    """
    charset = charset or "utf-8"
    content_type = content_type.lower() if content_type else ""

    if content_type.startswith("text/") or content_type == "application/json":
        payload = {
            "type": "text",
            "mime_type": content_type,
            "data": data.decode(charset),
        }
    elif content_type.startswith("image/"):
        payload = {
            "type": "image",
            "mime_type": content_type,
            "data_base64": base64.b64encode(data).decode(charset),
        }
    else:
        payload = {
            "type": "binary",
            "mime_type": content_type,
            "data_base64": base64.b64encode(data).decode(charset),
        }

    return ToolResult(structured_content=payload)