Skip to content

formatting

Class info

🛈 DocStrings

Utilities for working with pydantic-ai types and objects.

format_part

format_part(response: str | ModelRequestPart | ModelResponsePart) -> str

Format any kind of response part in a readable way.

Parameters:

Name Type Description Default
response str | ModelRequestPart | ModelResponsePart

Response part to format

required

Returns:

Type Description
str

A human-readable string representation

Source code in src/llmling_models/formatting.py
def format_part(  # noqa: PLR0911
    response: str | messages.ModelRequestPart | messages.ModelResponsePart,
) -> str:
    """Format any kind of response part in a readable way.

    Args:
        response: Response part to format

    Returns:
        A human-readable string representation
    """
    match response:
        case str():
            return response
        case messages.ToolCallPart(args=args, tool_name=tool_name):
            return f"Tool call: {tool_name}\nArgs: {args}"
        case messages.ToolReturnPart(outcome="retried", tool_name=tool_name) as part:
            return f"Retry needed for {tool_name}: {part.model_response_str(wrap_if_error=False)}"
        case messages.ToolReturnPart(tool_name=tool_name, content=content):
            return f"Tool {tool_name} returned: {content}"
        case messages.RetryFeedbackPart(content=str() as content):
            return f"Retry needed: {content}"
        case messages.RetryFeedbackPart(content=content):
            return f"Validation errors:\n{content}"
        case messages.UserPromptPart(content=content) if not isinstance(content, str):
            texts = []
            for item in content:
                match item:
                    case str():
                        texts.append(f"{item}")
                    case FileUrl(url=url):
                        texts.append(f"{url}")
                    case BinaryContent(identifier=identifier):
                        texts.append(f"Binary content: <{identifier}>")
            return "\n".join(texts)
        case (
            messages.SystemPromptPart(content=content)
            | messages.UserPromptPart(content=content)
            | messages.TextPart(content=content)
        ) if isinstance(content, str):
            return content
        case _:
            return str(response)