Home / Class/ AsyncJSONLDecoder Class — anthropic-sdk-python Architecture

AsyncJSONLDecoder Class — anthropic-sdk-python Architecture

Architecture documentation for the AsyncJSONLDecoder class in jsonl.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  ae5d86ac_c756_fcd6_d559_608e6815f980["AsyncJSONLDecoder"]
  e75927c6_4f5f_0cbb_8978_9029ad9f28aa["jsonl.py"]
  ae5d86ac_c756_fcd6_d559_608e6815f980 -->|defined in| e75927c6_4f5f_0cbb_8978_9029ad9f28aa
  330d2959_b903_77fe_23d4_b48eb13ad4a9["__init__()"]
  ae5d86ac_c756_fcd6_d559_608e6815f980 -->|method| 330d2959_b903_77fe_23d4_b48eb13ad4a9
  eb46ad18_d95e_9d3e_9e8e_232cd6cdffa2["close()"]
  ae5d86ac_c756_fcd6_d559_608e6815f980 -->|method| eb46ad18_d95e_9d3e_9e8e_232cd6cdffa2
  0a2eec90_25d4_ee12_1bd3_d5228d2858d8["__decode__()"]
  ae5d86ac_c756_fcd6_d559_608e6815f980 -->|method| 0a2eec90_25d4_ee12_1bd3_d5228d2858d8
  1b5fb7a8_a92d_d431_b0b0_633dd352d7b3["__anext__()"]
  ae5d86ac_c756_fcd6_d559_608e6815f980 -->|method| 1b5fb7a8_a92d_d431_b0b0_633dd352d7b3
  ad6ec737_52bb_d7c2_40a3_4cab3d6c7882["__aiter__()"]
  ae5d86ac_c756_fcd6_d559_608e6815f980 -->|method| ad6ec737_52bb_d7c2_40a3_4cab3d6c7882

Relationship Graph

Source Code

src/anthropic/_decoders/jsonl.py lines 70–123

class AsyncJSONLDecoder(Generic[_T]):
    """A decoder for [JSON Lines](https://jsonlines.org) format.

    This class provides an async iterator over a byte-iterator that parses each JSON Line
    into a given type.
    """

    http_response: httpx.Response

    def __init__(
        self,
        *,
        raw_iterator: AsyncIterator[bytes],
        line_type: type[_T],
        http_response: httpx.Response,
    ) -> None:
        super().__init__()
        self.http_response = http_response
        self._raw_iterator = raw_iterator
        self._line_type = line_type
        self._iterator = self.__decode__()

    async def close(self) -> None:
        """Close the response body stream.

        This is called automatically if you consume the entire stream.
        """
        await self.http_response.aclose()

    async def __decode__(self) -> AsyncIterator[_T]:
        buf = b""
        async for chunk in self._raw_iterator:
            for line in chunk.splitlines(keepends=True):
                buf += line
                if buf.endswith((b"\r", b"\n", b"\r\n")):
                    yield construct_type_unchecked(
                        value=json.loads(buf),
                        type_=self._line_type,
                    )
                    buf = b""

        # flush
        if buf:
            yield construct_type_unchecked(
                value=json.loads(buf),
                type_=self._line_type,
            )

    async def __anext__(self) -> _T:
        return await self._iterator.__anext__()

    async def __aiter__(self) -> AsyncIterator[_T]:
        async for item in self._iterator:
            yield item

Frequently Asked Questions

What is the AsyncJSONLDecoder class?
AsyncJSONLDecoder is a class in the anthropic-sdk-python codebase, defined in src/anthropic/_decoders/jsonl.py.
Where is AsyncJSONLDecoder defined?
AsyncJSONLDecoder is defined in src/anthropic/_decoders/jsonl.py at line 70.

Analyze Your Own Codebase

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

Try Supermodel Free