Home / Function/ _convert_annotation_from_v1() — langchain Function Reference

_convert_annotation_from_v1() — langchain Function Reference

Architecture documentation for the _convert_annotation_from_v1() function in _compat.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  b37385b6_2524_151c_2d19_4c6b96b5a5d4["_convert_annotation_from_v1()"]
  229dd2f8_cd21_122c_8dc9_e4e6685d089f["_compat.py"]
  b37385b6_2524_151c_2d19_4c6b96b5a5d4 -->|defined in| 229dd2f8_cd21_122c_8dc9_e4e6685d089f
  02f601bc_0436_a4b9_4ca2_139300c429ca["_convert_from_v1_to_anthropic()"]
  02f601bc_0436_a4b9_4ca2_139300c429ca -->|calls| b37385b6_2524_151c_2d19_4c6b96b5a5d4
  style b37385b6_2524_151c_2d19_4c6b96b5a5d4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/_compat.py lines 9–93

def _convert_annotation_from_v1(annotation: types.Annotation) -> dict[str, Any]:
    """Convert LangChain annotation format to Anthropic's native citation format."""
    if annotation["type"] == "non_standard_annotation":
        return annotation["value"]

    if annotation["type"] == "citation":
        if "url" in annotation:
            # web_search_result_location
            out: dict[str, Any] = {}
            if cited_text := annotation.get("cited_text"):
                out["cited_text"] = cited_text
            if "encrypted_index" in annotation.get("extras", {}):
                out["encrypted_index"] = annotation.get("extras", {})["encrypted_index"]
            if "title" in annotation:
                out["title"] = annotation["title"]
            out["type"] = "web_search_result_location"
            out["url"] = annotation.get("url")

            for key, value in annotation.get("extras", {}).items():
                if key not in out:
                    out[key] = value

            return out

        if "start_char_index" in annotation.get("extras", {}):
            # char_location
            out = {"type": "char_location"}
            for field in ["cited_text"]:
                if value := annotation.get(field):
                    out[field] = value
            if title := annotation.get("title"):
                out["document_title"] = title

            for key, value in annotation.get("extras", {}).items():
                out[key] = value
            out = {k: out[k] for k in sorted(out)}

            return out

        if "search_result_index" in annotation.get("extras", {}):
            # search_result_location
            out = {"type": "search_result_location"}
            for field in ["cited_text", "title"]:
                if value := annotation.get(field):
                    out[field] = value

            for key, value in annotation.get("extras", {}).items():
                out[key] = value

            return out

        if "start_block_index" in annotation.get("extras", {}):
            # content_block_location
            out = {}
            if cited_text := annotation.get("cited_text"):
                out["cited_text"] = cited_text
            if "document_index" in annotation.get("extras", {}):
                out["document_index"] = annotation.get("extras", {})["document_index"]
            if "title" in annotation:
                out["document_title"] = annotation["title"]

            for key, value in annotation.get("extras", {}).items():
                if key not in out:
                    out[key] = value

            out["type"] = "content_block_location"
            return out

        if "start_page_number" in annotation.get("extras", {}):
            # page_location
            out = {"type": "page_location"}
            for field in ["cited_text"]:
                if value := annotation.get(field):
                    out[field] = value
            if title := annotation.get("title"):
                out["document_title"] = title

            for key, value in annotation.get("extras", {}).items():
                out[key] = value

            return out

Domain

Subdomains

Frequently Asked Questions

What does _convert_annotation_from_v1() do?
_convert_annotation_from_v1() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/_compat.py.
Where is _convert_annotation_from_v1() defined?
_convert_annotation_from_v1() is defined in libs/partners/anthropic/langchain_anthropic/_compat.py at line 9.
What calls _convert_annotation_from_v1()?
_convert_annotation_from_v1() is called by 1 function(s): _convert_from_v1_to_anthropic.

Analyze Your Own Codebase

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

Try Supermodel Free