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

JSONLDecoder Class — anthropic-sdk-python Architecture

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

Entity Profile

Dependency Diagram

graph TD
  db3528f4_d56c_fc6e_955a_06508f166b4e["JSONLDecoder"]
  e75927c6_4f5f_0cbb_8978_9029ad9f28aa["jsonl.py"]
  db3528f4_d56c_fc6e_955a_06508f166b4e -->|defined in| e75927c6_4f5f_0cbb_8978_9029ad9f28aa
  d5b1c7a8_3677_7039_c835_0b8d0c179762["__init__()"]
  db3528f4_d56c_fc6e_955a_06508f166b4e -->|method| d5b1c7a8_3677_7039_c835_0b8d0c179762
  d5236247_8851_d026_c3db_8d02f9f4474e["close()"]
  db3528f4_d56c_fc6e_955a_06508f166b4e -->|method| d5236247_8851_d026_c3db_8d02f9f4474e
  5afca67b_7450_8f7a_3f11_b8ad0392063b["__decode__()"]
  db3528f4_d56c_fc6e_955a_06508f166b4e -->|method| 5afca67b_7450_8f7a_3f11_b8ad0392063b
  0255d477_7fd0_1ade_e136_204537e94c6e["__next__()"]
  db3528f4_d56c_fc6e_955a_06508f166b4e -->|method| 0255d477_7fd0_1ade_e136_204537e94c6e
  160ac0f7_3271_4673_bb61_0c815d59f54d["__iter__()"]
  db3528f4_d56c_fc6e_955a_06508f166b4e -->|method| 160ac0f7_3271_4673_bb61_0c815d59f54d

Relationship Graph

Source Code

src/anthropic/_decoders/jsonl.py lines 13–67

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

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

    http_response: httpx.Response
    """The HTTP response this decoder was constructed from"""

    def __init__(
        self,
        *,
        raw_iterator: Iterator[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__()

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

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

    def __decode__(self) -> Iterator[_T]:
        buf = b""
        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,
            )

    def __next__(self) -> _T:
        return self._iterator.__next__()

    def __iter__(self) -> Iterator[_T]:
        for item in self._iterator:
            yield item

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free