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

[Bug]: Presidio + Custom Logger: "AttributeError: 'list' object has no attribute 'usage'" #5682

Closed
nihal1294 opened this issue Sep 13, 2024 · 1 comment · Fixed by #5723 or #5731
Closed
Assignees
Labels
bug Something isn't working

Comments

@nihal1294
Copy link

nihal1294 commented Sep 13, 2024

What happened?

I am running the Proxy server in a k8s cluster with a custom logger callback to forward the token usage to another server while also using Presidio for PII masking and here is a part of the config yaml below:

litellm_settings:
    drop_params: True
    json_logs: True
    store_audit_logs: True
    log_raw_request_response: True
    return_response_headers: True
    num_retries: 5
    request_timeout: 200
    callbacks: ["presidio", "custom_logger.push_usage"]

I see a strange behavior where the response_obj in async_log_success_event is updated to type list instead of litellm.types.utils.ModelResponse which results in this error
AttributeError: 'list' object has no attribute 'usage'

for the following code snippet

async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
    print("Response obj type: ", type(response_obj))
    # input_tokens = response_obj.get("usage", {}).get("prompt_tokens", 0)
    # output_tokens = response_obj.get("usage", {}).get("completion_tokens", 0)
    input_tokens = response_obj.usage.prompt_tokens if hasattr(response_obj.usage, "prompt_tokens") else 0
    output_tokens = response_obj.usage.completion_tokens if hasattr(response_obj.usage, "completion_tokens") else 0
    ...

Initially, I tried to access the token counts via .get() as shown in the comments in the code above and later moved to the other way. But in both cases, I get the error shown above.

Strangely, I only see this happen when I am using Presidio callback, and without it, the response_obj type is litellm.types.utils.ModelResponse.

LiteLLM version: 1.44.24

This should be reproducible by using the Presidio callback to get the error, and without it to see no error. And also note the change in the datatype of response_obj.
Is this expected behavior or a bug?

Relevant log output

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/litellm/litellm_core_utils/litellm_logging.py", line 1503, in async_success_handler
    await callback.async_log_success_event(
  File "/data/custom_logger.py", line 13, in async_log_success_event
    input_tokens = response_obj.usage.prompt_tokens if hasattr(response_obj.usage, "prompt_tokens") else 0
                                                               ^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'usage'

Twitter

@nihal1294

@nihal1294 nihal1294 added the bug Something isn't working label Sep 13, 2024
@nihal1294 nihal1294 changed the title [Bug]: AttributeError: 'list' object has no attribute 'usage' [Bug]: Presidio + Custom Logger: "AttributeError: 'list' object has no attribute 'usage'" Sep 13, 2024
@krrishdholakia krrishdholakia self-assigned this Sep 14, 2024
@krrishdholakia
Copy link
Contributor

I think it might have to do with this -

async def async_logging_hook(

which returns

krrishdholakia added a commit that referenced this issue Sep 16, 2024
…ional presidio variables in guardrails config

Fixes #5682
krrishdholakia added a commit that referenced this issue Sep 17, 2024
* coverage (#5713)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* Move (#5714)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix(litellm_logging.py): fix logging client re-init (#5710)

Fixes #5695

* fix(presidio.py): Fix logging_hook response and add support for additional presidio variables in guardrails config

Fixes #5682

* feat(o1_handler.py): fake streaming for openai o1 models

Fixes #5694

* docs: deprecated traceloop integration in favor of native otel (#5249)

* fix: fix linting errors

* fix: fix linting errors

* fix(main.py): fix o1 import

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>
krrishdholakia added a commit that referenced this issue Sep 17, 2024
* LiteLLM Minor Fixes & Improvements (09/16/2024)  (#5723)

* coverage (#5713)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* Move (#5714)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix(litellm_logging.py): fix logging client re-init (#5710)

Fixes #5695

* fix(presidio.py): Fix logging_hook response and add support for additional presidio variables in guardrails config

Fixes #5682

* feat(o1_handler.py): fake streaming for openai o1 models

Fixes #5694

* docs: deprecated traceloop integration in favor of native otel (#5249)

* fix: fix linting errors

* fix: fix linting errors

* fix(main.py): fix o1 import

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>

* feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view (#5730)

* feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view

Supports having `MonthlyGlobalSpend` view be a material view, and exposes an endpoint to refresh it

* fix(custom_logger.py): reset calltype

* fix: fix linting errors

* fix: fix linting error

* fix: fix import

* test(test_databricks.py): fix databricks tests

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>
krrishdholakia added a commit that referenced this issue Sep 19, 2024
…m for fetching API base and token, if unspecified (#5746)

* LiteLLM Minor Fixes & Improvements (09/16/2024)  (#5723)

* coverage (#5713)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* Move (#5714)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix(litellm_logging.py): fix logging client re-init (#5710)

Fixes #5695

* fix(presidio.py): Fix logging_hook response and add support for additional presidio variables in guardrails config

Fixes #5682

* feat(o1_handler.py): fake streaming for openai o1 models

Fixes #5694

* docs: deprecated traceloop integration in favor of native otel (#5249)

* fix: fix linting errors

* fix: fix linting errors

* fix(main.py): fix o1 import

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>

* feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view (#5730)

* feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view

Supports having `MonthlyGlobalSpend` view be a material view, and exposes an endpoint to refresh it

* fix(custom_logger.py): reset calltype

* fix: fix linting errors

* fix: fix linting error

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix: fix import

* Fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* DB test

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* Coverage

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* progress

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix test name

Signed-off-by: dbczumar <corey.zumar@databricks.com>

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Krish Dholakia <krrishdholakia@gmail.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>
krrishdholakia added a commit that referenced this issue Sep 19, 2024
* fix(proxy_server.py): fix azure key vault logic to not require client id/secret

* feat(cost_calculator.py): support fireworks ai cost tracking

* build(docker-compose.yml): add lines for mounting config.yaml to docker compose

Closes #5739

* fix(input.md): update docs to clarify litellm supports content as a list of dictionaries

Fixes #5755

* fix(input.md): update input.md to include all message values

* fix(image_handling.py): follow image url redirects

Fixes #5763

* fix(router.py): Fix model key/base leak in error message

Fixes #5762

* fix(http_handler.py): fix linting error

* fix(azure.py): fix logging to show azure_ad_token being used

Fixes #5767

* fix(_redis.py): add redis sentinel support

Closes #4381

* feat(_redis.py): add redis sentinel support

Closes #4381

* test(test_completion_cost.py): fix test

* Databricks Integration: Integrate Databricks SDK as optional mechanism for fetching API base and token, if unspecified (#5746)

* LiteLLM Minor Fixes & Improvements (09/16/2024)  (#5723)

* coverage (#5713)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* Move (#5714)

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix(litellm_logging.py): fix logging client re-init (#5710)

Fixes #5695

* fix(presidio.py): Fix logging_hook response and add support for additional presidio variables in guardrails config

Fixes #5682

* feat(o1_handler.py): fake streaming for openai o1 models

Fixes #5694

* docs: deprecated traceloop integration in favor of native otel (#5249)

* fix: fix linting errors

* fix: fix linting errors

* fix(main.py): fix o1 import

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>

* feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view (#5730)

* feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view

Supports having `MonthlyGlobalSpend` view be a material view, and exposes an endpoint to refresh it

* fix(custom_logger.py): reset calltype

* fix: fix linting errors

* fix: fix linting error

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix: fix import

* Fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* DB test

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* Coverage

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* progress

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix

Signed-off-by: dbczumar <corey.zumar@databricks.com>

* fix test name

Signed-off-by: dbczumar <corey.zumar@databricks.com>

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Krish Dholakia <krrishdholakia@gmail.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>

* test: fix test

* test(test_databricks.py): fix test

* fix(databricks/chat.py): handle custom endpoint (e.g. sagemaker)

* Apply code scanning fix for clear-text logging of sensitive information

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* fix(__init__.py): fix known fireworks ai models

---------

Signed-off-by: dbczumar <corey.zumar@databricks.com>
Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com>
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants