Repository: pydantic/pydantic-ai
File: pydantic_evals/pydantic_evals/evaluators/llm_as_a_judge.py (Python)

Code before:
def _make_section(content: Any, tag: str) -> list[str | UserContent]:
    """Create a tagged section, handling different content types, for use in the LLMJudge's prompt.

    Args:
        content (Any): content to include in the section_
        tag (str): tag name for the section

    Returns:
        list[str | UserContent]: the tagged section as a list of strings or UserContent
    """
    sections: list[str | UserContent] = []
    if isinstance(content, str):
        sections.append(f'<{tag}>\n{content}\n</{tag}>')
    else:
        sections.append(f'<{tag}>')
        if isinstance(content, Sequence):
            for item in content:  # type: ignore[reportUnknownVariableType]
                if isinstance(item, str | MultiModalContent):
                    sections.append(item)
                else:
                    sections.append(_stringify(item))
        elif isinstance(content, MultiModalContent):
            sections.append(content)
        else:
            sections.append(_stringify(content))
        sections.append(f'</{tag}>')
    return sections


def _build_prompt(
    output: Any,
    rubric: str,
    inputs: Any | None = None,
    expected_output: Any | None = None,
) -> str | Sequence[str | UserContent]:
    """Build a prompt that includes input, output, expected output, and rubric."""
    sections: list[str | UserContent] = []
    if inputs is not None:
        sections.extend(_make_section(inputs, 'Input'))

    sections.extend(_make_section(output, 'Output'))
    sections.extend(_make_section(rubric, 'Rubric'))

    if expected_output is not None:
        sections.extend(_make_section(expected_output, 'ExpectedOutput'))
    if all(isinstance(section, str) for section in sections):
        return '\n'.join(sections)  # type: ignore[arg-type]
    return sections


Review comment (suggestion):
Now that we do the joining-if-all-strings at the end, I think this can be simplified as follows:

```suggestion
    content = content if isinstance(content, Sequence) else [content]
    
    sections.append(f'<{tag}>')
    for item in content:  # type: ignore[reportUnknownVariableType]
        if isinstance(item, str | MultiModalContent):
            sections.append(item)
        else:
            sections.append(_stringify(item))
    sections.append(f'</{tag}>')
```

Code after:
def _make_section(content: Any, tag: str) -> list[str | UserContent]:
    """Create a tagged section, handling different content types, for use in the LLMJudge's prompt.

    Args:
        content (Any): content to include in the section_
        tag (str): tag name for the section

    Returns:
        list[str | UserContent]: the tagged section as a list of strings or UserContent
    """
    sections: list[str | UserContent] = []
    content = content if isinstance(content, Sequence) and not isinstance(content, str) else [content]

    sections.append(f'<{tag}>')
    for item in content:
        sections.append(item if isinstance(item, str | MultiModalContent) else _stringify(item))
    sections.append(f'</{tag}>')
    return sections


def _build_prompt(
    output: Any,
    rubric: str,
    inputs: Any | None = None,
    expected_output: Any | None = None,
) -> str | Sequence[str | UserContent]:
    """Build a prompt that includes input, output, expected output, and rubric."""
    sections: list[str | UserContent] = []
    if inputs is not None:
        sections.extend(_make_section(inputs, 'Input'))

    sections.extend(_make_section(output, 'Output'))
    sections.extend(_make_section(rubric, 'Rubric'))

    if expected_output is not None:
        sections.extend(_make_section(expected_output, 'ExpectedOutput'))
    if all(isinstance(section, str) for section in sections):
        return '\n'.join(sections)  # type: ignore[arg-type]
    return sections