Home / File/ test_jsonl.py — anthropic-sdk-python Source File

test_jsonl.py — anthropic-sdk-python Source File

Architecture documentation for test_jsonl.py, a python file in the anthropic-sdk-python codebase. 5 imports, 0 dependents.

File python AnthropicClient Authentication 5 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  7e6f1ef1_47fa_dc28_4bd9_c457af975269["test_jsonl.py"]
  89ddcdd7_3ae1_4c7b_41bb_9f1e25f16875["typing"]
  7e6f1ef1_47fa_dc28_4bd9_c457af975269 --> 89ddcdd7_3ae1_4c7b_41bb_9f1e25f16875
  37c05070_ca59_d596_7250_de9d1939227f["typing_extensions"]
  7e6f1ef1_47fa_dc28_4bd9_c457af975269 --> 37c05070_ca59_d596_7250_de9d1939227f
  9c26e8a9_1ad2_1174_876a_1fc500ce0eaf["httpx"]
  7e6f1ef1_47fa_dc28_4bd9_c457af975269 --> 9c26e8a9_1ad2_1174_876a_1fc500ce0eaf
  cde8421b_93c7_41e4_d69d_2a3f1bade2f2["pytest"]
  7e6f1ef1_47fa_dc28_4bd9_c457af975269 --> cde8421b_93c7_41e4_d69d_2a3f1bade2f2
  e56e5c2b_2472_8d95_bc8c_c7fc6dc48507["anthropic._decoders.jsonl"]
  7e6f1ef1_47fa_dc28_4bd9_c457af975269 --> e56e5c2b_2472_8d95_bc8c_c7fc6dc48507
  style 7e6f1ef1_47fa_dc28_4bd9_c457af975269 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from typing import Any, Iterator, AsyncIterator
from typing_extensions import TypeVar

import httpx
import pytest

from anthropic._decoders.jsonl import JSONLDecoder, AsyncJSONLDecoder

_T = TypeVar("_T")


@pytest.mark.asyncio
@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
async def test_basic(sync: bool) -> None:
    def body() -> Iterator[bytes]:
        yield b'{"foo":true}\n'
        yield b'{"bar":false}\n'

    iterator = make_jsonl_iterator(
        content=body(),
        sync=sync,
        line_type=object,
    )

    assert await iter_next(iterator) == {"foo": True}
    assert await iter_next(iterator) == {"bar": False}

    await assert_empty_iter(iterator)


@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
async def test_new_lines_in_json(
    sync: bool,
) -> None:
    def body() -> Iterator[bytes]:
        yield b'{"content":"Hello, world!\\nHow are you doing?"}'

    iterator = make_jsonl_iterator(content=body(), sync=sync, line_type=object)

    assert await iter_next(iterator) == {"content": "Hello, world!\nHow are you doing?"}


@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
async def test_multi_byte_character_multiple_chunks(
    sync: bool,
) -> None:
    def body() -> Iterator[bytes]:
        yield b'{"content":"'
        # bytes taken from the string 'известни' and arbitrarily split
        # so that some multi-byte characters span multiple chunks
        yield b"\xd0"
        yield b"\xb8\xd0\xb7\xd0"
        yield b"\xb2\xd0\xb5\xd1\x81\xd1\x82\xd0\xbd\xd0\xb8"
        yield b'"}\n'

    iterator = make_jsonl_iterator(content=body(), sync=sync, line_type=object)

    assert await iter_next(iterator) == {"content": "известни"}


async def to_aiter(iter: Iterator[bytes]) -> AsyncIterator[bytes]:
    for chunk in iter:
        yield chunk


async def iter_next(iter: Iterator[_T] | AsyncIterator[_T]) -> _T:
    if isinstance(iter, AsyncIterator):
        return await iter.__anext__()
    return next(iter)


async def assert_empty_iter(decoder: JSONLDecoder[Any] | AsyncJSONLDecoder[Any]) -> None:
    with pytest.raises((StopAsyncIteration, RuntimeError)):
        await iter_next(decoder)


def make_jsonl_iterator(
    content: Iterator[bytes],
    *,
    sync: bool,
    line_type: type[_T],
) -> JSONLDecoder[_T] | AsyncJSONLDecoder[_T]:
    if sync:
        return JSONLDecoder(line_type=line_type, raw_iterator=content, http_response=httpx.Response(200))

    return AsyncJSONLDecoder(line_type=line_type, raw_iterator=to_aiter(content), http_response=httpx.Response(200))

Subdomains

Dependencies

  • anthropic._decoders.jsonl
  • httpx
  • pytest
  • typing
  • typing_extensions

Frequently Asked Questions

What does test_jsonl.py do?
test_jsonl.py is a source file in the anthropic-sdk-python codebase, written in python. It belongs to the AnthropicClient domain, Authentication subdomain.
What functions are defined in test_jsonl.py?
test_jsonl.py defines 7 function(s): assert_empty_iter, iter_next, make_jsonl_iterator, test_basic, test_multi_byte_character_multiple_chunks, test_new_lines_in_json, to_aiter.
What does test_jsonl.py depend on?
test_jsonl.py imports 5 module(s): anthropic._decoders.jsonl, httpx, pytest, typing, typing_extensions.
Where is test_jsonl.py in the architecture?
test_jsonl.py is located at tests/decoders/test_jsonl.py (domain: AnthropicClient, subdomain: Authentication, directory: tests/decoders).

Analyze Your Own Codebase

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

Try Supermodel Free