如何解决使用atproto连接Bluesky时出现的ModelError?
使用atproto连接Bluesky时遇到ValidationError问题
我尝试用atproto连接Bluesky,代码如下:
bsky_handle = "_____.bsky.social" bsky_password = "____-____-____-____" from atproto import Client bsky = Client() bsky.login(bsky_handle, bsky_password)
(代码取自某GitHub仓库)
但执行最后一行代码时触发了以下错误:
Traceback (most recent call last): File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\models\utils.py:87 in _get_or_create return model(**model_data) File ~\Anaconda3\anaconda3\lib\site-packages\pydantic\main.py:165 in __init__ __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__) ValidationError: 1 validation error for Response emailConfirmed Extra inputs are not permitted [type=extra_forbidden, input_value=True, input_type=bool] For further information visit https://errors.pydantic.dev/2.3/v/extra_forbidden The above exception was the direct cause of the following exception: Traceback (most recent call last): Cell In[12], line 2 bsky.login(bsky_handle, bsky_password) File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\client\client.py:74 in login session = self._get_and_set_session(login, password) File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\client\client.py:37 in _get_and_set_session session = self.com.atproto.server.create_session( File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\namespaces\sync_ns.py:1757 in create_session return get_response_model(response, models.ComAtprotoServerCreateSession.Response) File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\models\utils.py:98 in get_response_model return get_or_create(response.content, model) File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\models\utils.py:64 in get_or_create raise e File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\models\utils.py:57 in get_or_create model_instance = _get_or_create(model_data, model, strict=strict) File ~\Anaconda3\anaconda3\lib\site-packages\atproto\xrpc_client\models\utils.py:89 in _get_or_create raise ModelError(str(e)) from e ModelError: 1 validation error for Response emailConfirmed Extra inputs are not permitted [type=extra_forbidden, input_value=True, input_type=bool] For further information visit https://errors.pydantic.dev/2.3/v/extra_forbidden
问题原因
这不是你的操作错误,是atproto库的模型定义与Bluesky服务器返回的响应不匹配导致的。Bluesky服务器返回的会话响应中包含了emailConfirmed字段,但库内的Response模型未定义该字段,且开启了严格校验规则(不允许额外字段),因此触发了Pydantic的ValidationError。
解决方法
- 优先升级atproto库:开发者大概率已修复该字段匹配问题,执行以下命令更新库:
pip install --upgrade atproto - 临时修改模型校验规则(仅当升级无效时使用):
找到atproto库中ComAtprotoServerCreateSession.Response模型的定义文件(通常路径为site-packages/atproto/xrpc_client/models/com/atproto/server/create_session.py),修改模型的配置,让Pydantic忽略额外字段:
注意:修改库文件是临时方案,后续升级库会覆盖该修改,优先推荐升级库解决。class Response(BaseModel): access_jwt: str refresh_jwt: str handle: str did: str email_confirmed: Optional[bool] = Field(default=None, alias='emailConfirmed') model_config = ConfigDict(extra='ignore')
内容的提问来源于stack exchange,提问作者François M.
相关产品推荐
相关产品推荐

