# Buggy code
def prepare(self) -> None:
    for key, value in self.stream.get_headers().items():
        # Ignore Transfer-Encoding if the Content-Length has been set explicitly.
        if key.lower() == "transfer-encoding" and "content-length" in self.headers:
            continue
        self.headers.setdefault(key, value)

    auto_headers: typing.List[typing.Tuple[bytes, bytes]] = []

    has_host = "host" in self.headers
    has_content_length = (
        "content-length" in self.headers or "transfer-encoding" in self.headers
    )
    has_user_agent = "user-agent" in self.headers
    has_accept = "accept" in self.headers
    has_accept_encoding = "accept-encoding" in self.headers
    has_connection = "connection" in self.headers

    if not has_host:
        url = self.url
        if url.userinfo:
            url = url.copy_with(username=None, password=None)
        auto_headers.append((b"host", url.authority.encode("ascii")))
    if not has_content_length and self.method in ("POST", "PUT", "PATCH"):
        auto_headers.append((b"content-length", b"0"))
    if not has_user_agent:
        auto_headers.append((b"user-agent", USER_AGENT.encode("ascii")))
    if not has_accept:
        auto_headers.append((b"accept", b"*/*"))
    if not has_accept_encoding:
        auto_headers.append((b"accept-encoding", ACCEPT_ENCODING.encode()))
    if not has_connection:
        auto_headers.append((b"connection", b"keep-alive"))

    for item in reversed(auto_headers):
        self.headers.raw.insert(0, item)


# Traceback (KeyError)
import httpx
client = httpx.Client()
request = client.build_request("GET", "https://google.com")
requests.headers
Headers({'host': 'google.com', 'user-agent': 'python-httpx/0.14.1', 'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'connection': 'keep-alive'})
"host" in request.headers
False
request.headers.pop("host")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\programs\anaconda3\lib\_collections_abc.py", line 801, in pop
del self[key]
File "E:\projects\pycharm\httpx\httpx\_models.py", line 536, in __delitem__
del self._dict[del_key]
KeyError: b'host'

# Fixed code
def prepare(self) -> None:
    for key, value in self.stream.get_headers().items():
        # Ignore Transfer-Encoding if the Content-Length has been set explicitly.
        if key.lower() == "transfer-encoding" and "content-length" in self.headers:
            continue
        self.headers.setdefault(key, value)

    auto_headers: typing.List[typing.Tuple[bytes, bytes]] = []

    has_host = "host" in self.headers
    has_content_length = (
        "content-length" in self.headers or "transfer-encoding" in self.headers
    )
    has_user_agent = "user-agent" in self.headers
    has_accept = "accept" in self.headers
    has_accept_encoding = "accept-encoding" in self.headers
    has_connection = "connection" in self.headers

    if not has_host:
        url = self.url
        if url.userinfo:
            url = url.copy_with(username=None, password=None)
        auto_headers.append((b"host", url.authority.encode("ascii")))
    if not has_content_length and self.method in ("POST", "PUT", "PATCH"):
        auto_headers.append((b"content-length", b"0"))
    if not has_user_agent:
        auto_headers.append((b"user-agent", USER_AGENT.encode("ascii")))
    if not has_accept:
        auto_headers.append((b"accept", b"*/*"))
    if not has_accept_encoding:
        auto_headers.append((b"accept-encoding", ACCEPT_ENCODING.encode()))
    if not has_connection:
        auto_headers.append((b"connection", b"keep-alive"))

    self.headers = Headers(auto_headers + self.headers.raw)