s.py", line 186, in __call__ raise exc File "/home/jee/.copaw/venv/lib/python3.1...
🚨 错误信息
Traceback (most recent call last):
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 416, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/fastapi/applications.py", line 1160, in __call__
await super().__call__(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/applications.py", line 90, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 186, in __call__
raise exc
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 63, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/routing.py", line 660, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/routing.py", line 680, in app
await route.handle(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/fastapi/routing.py", line 130, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/fastapi/routing.py", line 116, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/fastapi/routing.py", line 670, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/fastapi/routing.py", line 324, in run_endpoint_function
return await dependant.call(**values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/copaw/app/routers/providers.py", line 207, in test_provider
ok = await tmp_provider.check_connection()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/copaw/providers/openai_provider.py", line 55, in check_connection
await client.models.list(timeout=timeout)
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/openai/_base_client.py", line 308, in _get_page
return await self._client.request(self._page_cls, self._options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/openai/_base_client.py", line 1674, in request
return await self._process_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/openai/_base_client.py", line 1763, in _process_response
return await api_response.parse()
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/openai/_response.py", line 435, in parse
parsed = self._options.post_parser(parsed)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jee/.copaw/venv/lib/python3.12/site-packages/openai/_base_client.py", line 299, in _parser
resp._set_private_attributes(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_set_private_attributes'
🤖 AI解决方案
报错分析报告
1.错误翻译
原文:AttributeError: 'str' object has no attribute '_set_private_attributes'
译文:属性错误:字符串对象没有'_set_private_attributes'属性
2. 原因分析
_set_private_attributes方法。 openai/_base_client.py第299行(_parser函数)尝试调用resp._set_private_attributes,但resp实际为字符串类型。 test_provider调用check_connection时,通过client.models.list()发起请求,响应解析失败。3. 解决方案
方案1:升级/降级OpenAI库版本(最常见原因)
bash
升级到最新稳定版
pip install --upgrade openai或降级到兼容版本(如1.3.0)
pip install openai==1.3.0
方案2:修复openai_provider.py的check_connection逻辑(若版本兼容仍有问题)
python
文件路径:/home/jee/.copaw/venv/lib/python3.12/site-packages/copaw/providers/openai_provider.py
async def check_connection(self, timeout: int = 10):
try:
# 原代码:await client.models.list(timeout=timeout)
# 修改为显式指定响应类型(或直接捕获异常)
models = await self.client.models.list(timeout=timeout)
return models is not None
except Exception as e:
# 打印详细错误以便排查
print(f"Connection check failed: {str(e)}")
return False
4. 预防措施
poetry/pip-tools锁定依赖版本,避免库间兼容性问题;定期运行pip check检查冲突。 try-except,记录原始响应内容(如print(resp)),快速定位类型错误。 推荐工具:pipdeptree(查看依赖树)、loguru(结构化日志)。