v36.0.0 regression fetching container logs
What happened (please include outputs or screenshots):
As of upgrading kubernetes to 36.0.0, something looks wrong in read_namespaced_pod_log
What you expected to happen:
Getting log lines out of my containers
How to reproduce it (as minimally and precisely as possible):
since_secs = 600
l = pod_api.read_namespaced_pod_log(namespace="foo-ns",
name="foo-pod",
container="foo-container",
follow=False,
timestamps=True,
since_seconds=600)
print("logs first chars:", l[:100])
Running with kubernetes 35.0.0, last line logged would say
logs first chars: 2026-05-25T07:52:34.381816807Z [25/05/2026 07:52:34] [MainThread] xxxx
Switching to 36.0.0. Same code outputs:
logs first chars: b'2026-05-24T15:57:23.246955706Z [24/05/2026 15:57:23] [MainThread]
Got confused at first, try to cast it or decode it into a string .... but no: this is a string already.
That b' are just the two first characters of my string.
Setting timestamps=False doesn't change anything, I'm still getting that b' prefix.
Anything else we need to know?:
Environment:
- Kubernetes version (
kubectl version): v1.31.14 - Python version (
python --version) 3.12.13 - Python client version (
pip list | grep kubernetes) 36.0.0
We are noticing another v36.0.0 regression in read_namespaced_pod_log - distinct from the bytes-vs-str return change reported above but for the same method.
We find that calling it through kubernetes.watch.Watch().stream(v1.read_namespaced_pod_log, ...) raises ApiTypeError: Got an unexpected keyword argument 'watch'.
Watch.stream passes watch= into the wrapped method, and v36's read_namespaced_pod_log no longer accepts it.
However, this worked on kubernetes < 36 with the same code.
File /opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/kubernetes/watch/watch.py:206, in Watch.stream(self, func, *args, **kwargs)
205 while True:
--> 206 resp = func(*args, **kwargs)
207 self._resp = resp
File /opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/kubernetes/client/api/core_v1_api.py:30578, in CoreV1Api.read_namespaced_pod_log(self, name, namespace, **kwargs)
30576 """
30577 kwargs['_return_http_data_only'] = True
> 30578 return self.read_namespaced_pod_log_with_http_info(name, namespace, **kwargs) # noqa: E501
File /opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/kubernetes/client/api/core_v1_api.py:30668, in CoreV1Api.read_namespaced_pod_log_with_http_info(self, name, namespace, **kwargs)
30666 for key, val in six.iteritems(local_var_params['kwargs']):
30667 if key not in all_params:
> 30668 raise ApiTypeError(
30669 "Got an unexpected keyword argument '%s'"
ApiTypeError: Got an unexpected keyword argument 'watch' to method read_namespaced_pod_log
Ref: https://github.com/kubeflow/trainer/pull/3359#issuecomment-4527185324 https://github.com/kubeflow/trainer/pull/3359#discussion_r3298322822
/cc @aojea
👀 , logs does not use websocket IIRC, exec and portforward do ... will take a closer look tomorrow
https://github.com/kubernetes-client/python/releases/tag/v36.0.2 has been released, please check if it helps fix the problem.
In https://github.com/kubernetes-client/python/pull/2486/ it indeed detects if the stream is binary or not and switches the output to the format, but logs does not use websocket
https://github.com/kubernetes/kubernetes/blob/master/pkg/registry/core/pod/rest/log.go
kubectl -n sam-bananas logs sam-hub-bananas-8649dc7669-d6j6f -v 7
I0605 15:21:21.875176 67594 loader.go:405] Config loaded from file: .kube/config
I0605 15:21:21.879770 67594 round_trippers.go:527] "Request" verb="GET" url="https://X.X.X.X/api/v1/namespaces/sam-bananas/pods/sam-hub-bananas-8649dc7669-d6j6f" headers=<
Accept: application/json, */*
User-Agent: kubectl/v1.35.3 (linux/amd64) kubernetes/665c2a2
>
I0605 15:21:22.022906 67594 round_trippers.go:632] "Response" status="200 OK" milliseconds=143
I0605 15:21:22.025890 67594 round_trippers.go:527] "Request" verb="GET" url="https://X.X.X.X/api/v1/namespaces/sam-bananas/pods/sam-hub-bananas-8649dc7669-d6j6f/log?container=sam-hub" headers=<
Accept: application/json, */*
User-Agent: kubectl/v1.35.3 (linux/amd64) kubernetes/665c2a2
>
I0605 15:21:22.199574 67594 round_trippers.go:632] "Response" status="200 OK" milliseconds=173
@yliaog it seems that method is autogenerated from the openapi directtly https://github.com/kubernetes-client/python/blob/master/kubernetes/client/api/core_v1_api.py , I wonder if there is a problem with the generator or with the openapi itself
The original issue by @faust64 doesn't appear to be fixed.
/assign @yliaog
It looks like the change in PR 2486 is incorrectly treating the response stream as binary even for non-websocket calls like read_namespaced_pod_log. Since the library now prefixes the output with b' based on the stream type detection, the API response is likely being coerced into a bytes representation of a string rather than the raw string content itself. Could you check if casting the response to a stream or explicitly setting the _preload_content flag changes how the bytes are returned? If the underlying rest client is misinterpreting the Content-Type header from the K8s API server, we may need to force a decode on the response object before it hits the consumer code.
same here, please give it attention
@aojea could you please take a look the comment above: https://github.com/kubernetes-client/python/issues/2596#issuecomment-5561582452
If the regression persists in v36.0.2, the presence of the b' prefix suggests that the underlying stream handling is inadvertently treating the response as a raw byte stream rather than decoding it as utf-8. Since read_namespaced_pod_log is expected to return a string, could you verify if the response headers are being parsed correctly in the new version, specifically checking if the Content-Type is being interpreted as application/octet-stream? It might be useful to inspect the urllib3 response object directly to see if the raw data is being returned as bytes before the client-side conversion logic processes it.
https://github.com/kubernetes-client/python/releases/tag/v36.0.2 has been released, please check if it helps fix the problem.
@aojea could you please take a look the comment above: https://github.com/kubernetes-client/python/issues/2596#issuecomment-5561582452
It's still an issue in 36.0.3.
The presence of the b' prefix suggests that read_namespaced_pod_log is returning a raw byte string that is being misinterpreted as a standard string. Since v36.0.2 did not resolve this, could you verify if the underlying urllib3 response stream is bypassing the expected decoding layer now that the client handles binary streams differently?
The b' prefix appearing in your output suggests the client is incorrectly interpreting the raw byte stream as a string literal rather than decoding the response body. Since the linked PR 2486 introduced logic to toggle stream handling, are you seeing the same behavior if you manually force the response to be processed as bytes before decoding?
or the HTTP response handler is callingstr()on the raw bytes response instead of.decode('utf-8
The regression likely stems from the library incorrectly handling the byte stream as a string literal, resulting in the literal characters b' being prepended to the output buffer in v36.0.0. Since read_namespaced_pod_log does not use websockets, does the internal stream handling logic introduced in the recent release assume a binary response type regardless of the endpoint being hit?
Thanks for the follow-up. To clarify what is happening here:
This is not related to websockets or PR #2486 (container logs do not use websockets).
Root Cause
In PR #2532 (OpenAPI Generator v6.6.0 upgrade), commit 54be1a86 added 'str' to the exclusion list in kubernetes/client/api_client.py, and a8758f3a captured that change in scripts/api_client_patch.diff so it is re-applied on regeneration:
- if six.PY3 and response_type not in ["file", "bytes"]:
+ if six.PY3 and response_type not in ["file", "bytes", "str"]:
In v35, rest.py unconditionally decoded response bodies (r.data = r.data.decode('utf8')). When that decode was removed from rest.py in v36, adding 'str' to the exclusion in api_client.py meant that endpoints returning response_type='str' (like read_namespaced_pod_log) never have response_data.data decoded from bytes to str.
When deserialize(response_data, 'str') runs:
- json.loads(response.data) fails because container logs are plain text, not JSON.
- It falls back to __deserialize_primitive(data, str), which executes str(data).
- In Python 3, calling str() on a bytes object returns its representation b'...', prepending literal b' to the string.
Status & Fix
- In v37.0.0+ / master, OpenAPI Generator was updated to 7.25, where response_deserialize properly decodes text responses when response_type == 'str'.
- For release-36.0, we can't simply drop 'str' from the exclusion: stream() exec calls go through the same code path with response_type 'str'. Their WSResponse.data is already a str for text output (so .decode() would raise), and binary=True relies on the str(bytes) repr (stream.py strips out[2:-1]). Instead, the backport should decode only HTTP responses, e.g. by adding isinstance(response_data, rest.RESTResponse) to that condition, and leave WSResponse untouched.
- Workaround: Call read_namespaced_pod_log with _preload_content=False and decode the body explicitly, e.g. resp.data.decode('utf-8').
It looks like read_namespaced_pod_log is executing str() on the raw bytes
FYI, I noticed my automation auto-merged 37.0.1, on that controller where I was stuck reading pods logs. integration tests passed, I can see it running today in prod ... This was fixed, then?
Thanks @roycaihw for that explanation. Closing, then.