Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/crewai/src/crewai/llms/base_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class BaseLLM(BaseModel, ABC):
)
additional_params: dict[str, Any] = Field(default_factory=dict)

def __repr__(self) -> str:
"""Return a representation with sensitive fields masked."""
d = self.model_dump()
if d.get("api_key"):
d["api_key"] = "***"
return f"{self.__class__.__name__}({d})"

@field_serializer("response_format", when_used="json", check_fields=False)
def _serialize_response_format(self, value: Any) -> Any:
return serialize_model_class(value)
Expand Down
23 changes: 22 additions & 1 deletion lib/crewai/tests/test_custom_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,31 @@ def get_context_window_size(self) -> int:
"""
return 4096

async def acall(self, messages, tools=None, callbacks=None, available_functions=None, from_task=None, from_agent=None, response_model=None):
async def acall(
self,
messages,
tools=None,
callbacks=None,
available_functions=None,
from_task=None,
from_agent=None,
response_model=None,
):
"""Raise because async calls are not implemented for this test LLM."""
raise NotImplementedError


def test_base_llm_repr_masks_api_key():
"""Test that BaseLLM repr does not expose the API key."""
llm = CustomLLM()
llm.api_key = "sk-secret"

representation = repr(llm)

assert "sk-secret" not in representation
assert "'api_key': '***'" in representation


@pytest.mark.vcr()
def test_custom_llm_implementation():
"""Test that a custom LLM implementation works with create_llm."""
Expand Down