libs/partners/ollama/langchain_ollama/_compat.py PYTHON 68 lines View on github.com → Search inside
1"""Go from v1 content blocks to Ollama SDK format."""23from typing import Any45from langchain_core.messages import content as types678def _convert_from_v1_to_ollama(9    content: list[types.ContentBlock],10    model_provider: str | None,  # noqa: ARG00111) -> list[dict[str, Any]]:12    """Convert v1 content blocks to Ollama format.1314    Args:15        content: List of v1 `ContentBlock` objects.16        model_provider: The model provider name that generated the v1 content.1718    Returns:19        List of content blocks in Ollama format.20    """21    new_content: list = []22    for block in content:23        if not isinstance(block, dict) or "type" not in block:24            continue2526        block_dict = dict(block)  # (For typing)2728        # TextContentBlock29        if block_dict["type"] == "text":30            # Note: this drops all other fields/extras31            new_content.append({"type": "text", "text": block_dict["text"]})3233        # ReasoningContentBlock34        # Ollama doesn't take reasoning back in35        # In the future, could consider coercing into text as an option?36        # e.g.:37        # if block_dict["type"] == "reasoning":38        #     # Attempt to preserve content in text form39        #     new_content.append({"text": str(block_dict["reasoning"])})4041        # ImageContentBlock42        if block_dict["type"] == "image":43            # Already handled in _get_image_from_data_content_block44            new_content.append(block_dict)4546        # TODO: AudioContentBlock once models support4748        # TODO: FileContentBlock once models support4950        # ToolCall -> ???51        # if block_dict["type"] == "tool_call":52        #     function_call = {}53        #     new_content.append(function_call)5455        # ToolCallChunk -> ???56        # elif block_dict["type"] == "tool_call_chunk":57        #     function_call = {}58        #     new_content.append(function_call)5960        # NonStandardContentBlock61        if block_dict["type"] == "non_standard":62            # Attempt to preserve content in text form63            new_content.append(64                {"type": "text", "text": str(block_dict.get("value", ""))}65            )6667    return new_content

Code quality findings 1

Overuse may indicate design issues; consider polymorphism
isinstance-overuse
if not isinstance(block, dict) or "type" not in block:

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.