gleanwork / gleanwork/api-client-python

upload_files()/upload_files_async() raises httpx.RequestNotRead

未關閉 適合新手
#136 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Python
星號
20
分支
10
平均合併
1 天 6 小時
30 天內合併 PR
17

描述

Description
The official example examples/agent_with_file_upload.py fails with httpx.RequestNotRead when calling glean.client.chat.upload_files().

Steps to reproduce
Run the official example as-is:

from glean.api_client import Glean, models
with Glean(api_token="...", instance="...") as glean:
upload_result = glean.client.chat.upload_files(
files=[models.File(file_name="test.csv", content=b"a,b\n1,2")]
)
All content variants produce the same error: bytes, io.BytesIO, open(..., 'rb'), FileTypedDict.

Error
httpx.RequestNotRead: Attempted to access streaming request content, without having called read().

Root cause
The XGlean BeforeRequestHook always reconstructs the httpx.Request object to add optional headers:

glean/api_client/_hooks/init.py — XGlean.before_request()

return httpx.Request(
method=request.method,
url=request.url,
headers=new_headers,
content=request.content, # <-- raises RequestNotRead
extensions=request.extensions,
)
For multipart requests (file uploads), httpx builds the body as a stream. Accessing request.content before request.read() raises httpx.RequestNotRead. This happens on every upload_files() / upload_files_async() call, regardless of how file content is passed, because the hook runs before the request is sent.

Non-multipart endpoints (JSON body) are unaffected because request.content is available immediately.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 glean/api_client/_hooks/init.py 中的 XGlean.before_request() 開始,接著檢視 examples/agent_with_file_upload.py,並使用文件中記載的輸入重現失敗的上傳。完成的標準是:upload_files() 和 upload_files_async() 能夠處理列出的內容變體,同時非 multipart JSON 端點仍不受影響。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api
Issue 類型
缺陷
難度
2/5
預估耗時
1-3 小時
活躍度
冷清
描述清晰度
描述清楚
新手友好度
78/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。