Skip to content

Conversation

@ekzhu
Copy link
Collaborator

@ekzhu ekzhu commented Nov 1, 2024

Resolves #4017

Copy link
Collaborator

@victordibia victordibia left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!!

@ekzhu ekzhu merged commit ca7caa7 into main Nov 1, 2024
36 checks passed
@ekzhu ekzhu deleted the agentchat-token-usage branch November 1, 2024 20:20
@auphof
Copy link
Contributor

auphof commented Nov 4, 2024

@ekzhu with the introduction of

I am experiencing a flurry of pydantic UserWarnings like Field "model_usage" in BaseMessage has conflict with protected namespace "model_".

This happens when i attempt something like from autogen_agentchat.logging import ConsoleLogHandler

image

I have managed to suppress it with doing the following model_config in BaseMessage

from pydantic import BaseModel, ConfigDict

class BaseMessage(BaseModel):
    """A base message."""

    # Work around to suppress protected namespace  checks, 
    # Without out this we get`UserWarnings`` like `Field "model_usage" in BaseMessage has conflict with protected namespace "model_".` `
    model_config = ConfigDict(protected_namespaces=())

    source: str
    """The name of the agent that sent this message."""

    model_usage: RequestUsage | None = None
    """The model client usage incurred when producing this message."""

However as per https://docs.pydantic.dev/2.1/usage/model_config/#protected-namespaces
By setting protected_namespaces=(), you're essentially disabling the protection for all namespaces, which allows you to use field names that would otherwise conflict with Pydantic's protected namespaces.

So, it's important to note a few things:

  • This approach removes all namespace protections, which might lead to unexpected behavior if you're not careful with your field names.
  • If possible, consider renaming the field to avoid the conflict altogether. For example, you could use usage_data instead of model_usage.

@ekzhu
Copy link
Collaborator Author

ekzhu commented Nov 4, 2024

@auphof thanks for the catch, we will rename the field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include Metric in AgentChat Messages

4 participants