Home / File/ bedrock_converse.py — langchain Source File

bedrock_converse.py — langchain Source File

Architecture documentation for bedrock_converse.py, a python file in the langchain codebase. 5 imports, 0 dependents.

File python CoreAbstractions MessageSchema 5 imports 8 functions

Entity Profile

Dependency Diagram

graph LR
  c5727b5d_0ac6_f95f_aa3f_0910508283f3["bedrock_converse.py"]
  66c6348c_7716_027c_42d7_71449bc64eeb["base64"]
  c5727b5d_0ac6_f95f_aa3f_0910508283f3 --> 66c6348c_7716_027c_42d7_71449bc64eeb
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  c5727b5d_0ac6_f95f_aa3f_0910508283f3 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  c5727b5d_0ac6_f95f_aa3f_0910508283f3 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  c5727b5d_0ac6_f95f_aa3f_0910508283f3 --> d758344f_537f_649e_f467_b9d7442e86df
  23170be8_e8a8_a728_6471_2a7fe6195245["langchain_core.messages.block_translators"]
  c5727b5d_0ac6_f95f_aa3f_0910508283f3 --> 23170be8_e8a8_a728_6471_2a7fe6195245
  style c5727b5d_0ac6_f95f_aa3f_0910508283f3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Derivations of standard content blocks from Amazon (Bedrock Converse) content."""

import base64
from collections.abc import Iterator
from typing import Any, cast

from langchain_core.messages import AIMessage, AIMessageChunk
from langchain_core.messages import content as types


def _bytes_to_b64_str(bytes_: bytes) -> str:
    return base64.b64encode(bytes_).decode("utf-8")


def _populate_extras(
    standard_block: types.ContentBlock, block: dict[str, Any], known_fields: set[str]
) -> types.ContentBlock:
    """Mutate a block, populating extras."""
    if standard_block.get("type") == "non_standard":
        return standard_block

    for key, value in block.items():
        if key not in known_fields:
            if "extras" not in standard_block:
                # Below type-ignores are because mypy thinks a non-standard block can
                # get here, although we exclude them above.
                standard_block["extras"] = {}  # type: ignore[typeddict-unknown-key]
            standard_block["extras"][key] = value  # type: ignore[typeddict-item]

    return standard_block


def _convert_to_v1_from_converse_input(
    content: list[types.ContentBlock],
) -> list[types.ContentBlock]:
    """Convert Bedrock Converse format blocks to v1 format.

    During the `content_blocks` parsing process, we wrap blocks not recognized as a v1
    block as a `'non_standard'` block with the original block stored in the `value`
    field. This function attempts to unpack those blocks and convert any blocks that
    might be Converse format to v1 ContentBlocks.

    If conversion fails, the block is left as a `'non_standard'` block.

    Args:
        content: List of content blocks to process.

    Returns:
        Updated list with Converse blocks converted to v1 format.
    """

    def _iter_blocks() -> Iterator[types.ContentBlock]:
        blocks: list[dict[str, Any]] = [
            cast("dict[str, Any]", block)
            if block.get("type") != "non_standard"
            else block["value"]  # type: ignore[typeddict-item]  # this is only non-standard blocks
            for block in content
        ]
        for block in blocks:
            num_keys = len(block)
// ... (260 more lines)

Subdomains

Dependencies

  • base64
  • collections.abc
  • langchain_core.messages
  • langchain_core.messages.block_translators
  • typing

Frequently Asked Questions

What does bedrock_converse.py do?
bedrock_converse.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in bedrock_converse.py?
bedrock_converse.py defines 8 function(s): _bytes_to_b64_str, _convert_citation_to_v1, _convert_to_v1_from_converse, _convert_to_v1_from_converse_input, _populate_extras, _register_bedrock_converse_translator, translate_content, translate_content_chunk.
What does bedrock_converse.py depend on?
bedrock_converse.py imports 5 module(s): base64, collections.abc, langchain_core.messages, langchain_core.messages.block_translators, typing.
Where is bedrock_converse.py in the architecture?
bedrock_converse.py is located at libs/core/langchain_core/messages/block_translators/bedrock_converse.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/langchain_core/messages/block_translators).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free