litellm
litellm copied to clipboard
Fix issue #2832: Add protected_namespaces to Config class within utils.py, router.py and completion.py to avoid the warning message.
Fixes #2832
Problem
The library was using Pydantic, and some of its models had attribute names that conflicted with Pydantic's protected namespaces. This caused warning messages to be generated when using these models. The affected files were:
types/router.pytypes/completion.pyutils.py
Solution
To resolve the namespace conflicts and suppress the warning messages, I added a configuration to the affected models to disable Pydantic's namespace protection for these specific models. This allows the use of attribute names that conflict with the protected namespaces without triggering warnings.
class Config:
protected_namespaces = ()
By setting protected_namespaces to an empty tuple (), Pydantic's namespace protection is disabled for these specific models. This allows the use of attribute names that conflict with the protected namespaces without triggering warnings.
The changes were made in the following files:
types/router.pytypes/completion.pyutils.py
Testing
To ensure that the changes resolved the issue, I ran the affected code and verified that no warning messages related to Pydantic namespace conflicts were generated.
Additional Notes
Disabling the namespace protection should be done with caution, as it may potentially lead to naming conflicts if attribute names unintentionally match the protected namespaces. However, in this case, the attribute names causing the conflicts were intentional and necessary for the library's functionality.
Please let me know if you have any questions or feedback regarding this pull request. Thank you for considering this contribution!
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| litellm | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Apr 8, 2024 5:00am |
is this going to be in 1.34.38? ETA for release?
is this going to be in 1.34.38? ETA for release?
ETA is latest by EOD today
Awesome, Thank you!
Suggestions on how we can avoid this warning in the future?
Maybe a simple CI check making sure import litellm does not generate any warnings or output in general?
@ishaan-jaff Since version 2, Pydantic added model_ to their protected namespace, a common term in ML. One workaround is adding protected_namespaces = () in the config section of any model using model_ which I did here.
However there is one funny hack. Alternatively, you can intercept the global log in Python to bypass this message. Here’s a sample for you:
import warnings
import logging
def custom_warning_handler(message, category, filename, lineno, file=None, line=None):
# Check the warning message or category here
if 'Field "model_name" has conflict with protected namespace' in str(message):
pass
else:
original_showwarning(message, category, filename, lineno, file, line)
# Save a reference to the original showwarning function
original_showwarning = warnings.showwarning
# Override the showwarning function with your custom handler
warnings.showwarning = custom_warning_handler
## ===================================
from pydantic import BaseModel
from typing import Optional
from pydantic.fields import Field
class Device(BaseModel):
model_name: Optional[str] = Field(None)
data = {
'model_name': "ab",
}
m = Device(**data)
print(m)
It's like instead of cleaning your room, you just shove all the trash under the bed 😄. I know it's lame, but hey, it works!
I still see an issue on main-v1.35.31. Is this related?
litellm-1 | /usr/local/lib/python3.11/site-packages/pydantic/_internal/_fields.py:160: UserWarning: Field "model_name" has conflict with protected namespace "model_".
litellm-1 |
litellm-1 | You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
litellm-1 | warnings.warn(
litellm-1 | /usr/local/lib/python3.11/site-packages/pydantic/_internal/_fields.py:160: UserWarning: Field "model_info" has conflict with protected namespace "model_".
litellm-1 |
litellm-1 | You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
I will start to try to fix following errors in current v1.35.32.dev1.
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:219
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:219: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:306
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:306: PydanticDeprecatedSince20: `pydantic.config.Extra` is deprecated, use literal values instead (e.g. `extra='allow'`). Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
extra = Extra.allow # Allow extra fields
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:309
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:309: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:338
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:338: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:385
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:385: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:451
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:451: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:463
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:463: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:503
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:503: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:537
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:537: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:828
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:828: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:855
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:855: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)
../../../.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:874
/home/runner/.cache/pypoetry/virtualenvs/opendevin-cQohdhQS-py3.11/lib/python3.11/site-packages/litellm/proxy/_types.py:874: PydanticDeprecatedSince20: Pydantic V1 style `@root_validator` validators are deprecated. You should migrate to Pydantic V2 style `@model_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/
@root_validator(pre=True)