Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #2832: Add protected_namespaces to Config class within utils.py, router.py and completion.py to avoid the warning message. #2893

Merged
merged 2 commits into from
Apr 9, 2024

Conversation

unclecode
Copy link
Contributor

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.py
  • types/completion.py
  • utils.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.py
  • types/completion.py
  • utils.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!

…in utils.py, router.py and completion.py to avoid the warning message.
…nother to Config class within the router.py
Copy link

vercel bot commented Apr 8, 2024

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

@unclecode unclecode changed the title Fix issue ix issue #2832: Add protected_namespaces to Config class within utils.py, router.py and completion.py to avoid the warning message. Fix issue #2832: Add protected_namespaces to Config class within utils.py, router.py and completion.py to avoid the warning message. Apr 8, 2024
Copy link
Contributor

@ishaan-jaff ishaan-jaff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm ! @unclecode do you have suggestions on how we can avoid this warning in the future

@ishaan-jaff ishaan-jaff merged commit caec537 into BerriAI:main Apr 9, 2024
1 check passed
@foragerr
Copy link
Contributor

foragerr commented Apr 9, 2024

is this going to be in 1.34.38? ETA for release?

@ishaan-jaff
Copy link
Contributor

is this going to be in 1.34.38? ETA for release?

ETA is latest by EOD today

@foragerr
Copy link
Contributor

foragerr commented Apr 9, 2024

Awesome, Thank you!

@nicovank
Copy link
Contributor

nicovank commented Apr 9, 2024

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?

@unclecode
Copy link
Contributor Author

@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!

@patrickwasp
Copy link

patrickwasp commented Apr 30, 2024

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'] = ()`.

@Umpire2018
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Multiple Pydantic warnings, field model_{id|name|info} has conflict with protected namespace model_
6 participants