```python:inference/core/workflows/execution_engine/v1/dynamic_blocks/block_scaffolding.py
from functools import lru_cache
from typing import Optional


@lru_cache(maxsize=128)
def _cached_splitlines(code: str) -> list[str]:
    # Cache splitlines results for recently-seen code strings to avoid
    # repeated allocation of identical lists across many calls.
    return code.splitlines()

def build_traceback_string(
    code: Optional[str],
    line_number: int,
    function_name: str,
    error_type: str,
    error_msg: str,
) -> str:
    """Build a traceback string from structured error data."""
    # Use cached splitlines for performance when same code string recurs.
    code_lines = _cached_splitlines((code or ""))
    code_line = (
        code_lines[line_number - 1].strip()
        if 0 < line_number <= len(code_lines)
        else ""
    )

    lines = [
        "Traceback (most recent call last):",
        f'  File "Python Block", line {line_number}, in {function_name}',
    ]
    if code_line:
        lines.append(f"    {code_line}")
    lines.append(f"{error_type}: {error_msg}")
    return "\n".join(lines)
```