#4617·litestar

Bug: Using structlog's KeyValueRenderer causes an error on every message

Author: xealsCreated Mar 1, 2026Updated Sep 30, 2026
LabelsBug :bug:

Description

This is the same symptoms as #2151, but occurs with structlog.processors.KeyValueRenderer as well. Specifically it only occurs when not attached to a TTY, such as being run through nohup or a system service manager.

URL to code causing the issue

No response

MCVE

python
import structlog
from litestar import Litestar, Request, get
from litestar.logging import StructLoggingConfig
from litestar.plugins.structlog import StructlogConfig, StructlogPlugin

logger = structlog.get_logger()


@get("/")
async def my_router_handler(request: Request) -> None:
    logger.info("inside a request")
    return None


# Only change to base example, added two parameters to config.
logging_config = StructlogConfig(
    structlog_logging_config=StructLoggingConfig(
        processors=[structlog.processors.KeyValueRenderer()],
        log_exceptions="always",
    ),
)

app = Litestar(
    route_handlers=[my_router_handler],
    plugins=[StructlogPlugin(logging_config)],
)

Steps to reproduce

  1. nohup uvicorn mvce:app
  2. curl localhost:8000
  3. Observe Internal Server Error and confirm backtrace in nohup.log

Screenshots

No response

Logs

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/middleware/_internal/
exceptions/middleware.py", line 158, in __call__
    await self.app(scope, receive, capture_response_started)
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/_asgi/asgi_router.py"
, line 100, in __call__
    await asgi_app(scope, receive, send)
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/middleware/base.py",
line 147, in wrapped_call
    await original__call__(self, scope, receive, send)  # pyright: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/middleware/logging.py
", line 110, in __call__
    await self.log_request(scope=scope, receive=receive)
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/middleware/logging.py
", line 125, in log_request
    self.log_message(values=extracted_data)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/middleware/logging.py
", line 150, in log_message
    self.logger.info(message, **values)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/structlog/_native.py", line 16
4, in meth
    return self._proxy_to_logger(
           ~~~~~~~~~~~~~~~~~~~~~^
        name, _maybe_interpolate(event, args), **kw
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/structlog/_base.py", line 223,
 in _proxy_to_logger
    return getattr(self._logger, method_name)(*args, **kw)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/structlog/_output.py", line 32
1, in msg
    self._write(message + b"\n")
                ~~~~~~~~^~~~~~~
TypeError: can only concatenate str (not "bytes") to str

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        self.scope, self.receive, self.send
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/uvicorn/middleware/proxy_heade
rs.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/app.py", line 623, in
 __call__
    await self.asgi_handler(scope, receive, self._wrap_send(send=send, scope=scope))  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/middleware/_internal/
exceptions/middleware.py", line 166, in __call__
    self.handle_exception_logging(logger=logger, logging_config=litestar_app.logging_config, scope=scope)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/middleware/_internal/
exceptions/middleware.py", line 270, in handle_exception_logging
    logging_config.exception_logging_handler(logger, scope, format_exception(*exc))
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/litestar/logging/config.py", l
ine 137, in _default_exception_logging_handler
    logger.exception(
    ~~~~~~~~~~~~~~~~^
        "Uncaught exception",
        ^^^^^^^^^^^^^^^^^^^^^
        connection_type=scope["type"],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        path=scope["path"],
        ^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/structlog/_native.py", line 47
, in exception
    return self.error(event, *args, **kw)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/structlog/_native.py", line 16
4, in meth
    return self._proxy_to_logger(
           ~~~~~~~~~~~~~~~~~~~~~^
        name, _maybe_interpolate(event, args), **kw
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/structlog/_base.py", line 223, in _proxy_to_logger
    return getattr(self._logger, method_name)(*args, **kw)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/nix/store/ggwbxm2ambk0ihill7r12c8vvjsiszxr-dev-env/lib/python3.13/site-packages/structlog/_output.py", line 321, in msg
    self._write(message + b"\n")
                ~~~~~~~~^~~~~~~
TypeError: can only concatenate str (not "bytes") to str

Litestar Version

2.19.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)