add assertions for logged slp object

This commit is contained in:
Ishaan Jaff 2025-04-28 15:04:02 -07:00
parent dfc4b49064
commit 8e2dacaf4f
2 changed files with 36 additions and 2 deletions

View file

@ -4492,10 +4492,13 @@ async def amoderation(
_openai_client = openai_client
optional_params = GenericLiteLLMParams(**kwargs)
litellm_logging_obj: Optional[LiteLLMLoggingObj] = kwargs.get(
"litellm_logging_obj", None
)
try:
(
model,
_custom_llm_provider,
custom_llm_provider,
_dynamic_api_key,
_dynamic_api_base,
) = litellm.get_llm_provider(
@ -4507,6 +4510,20 @@ async def amoderation(
except litellm.BadRequestError:
# `model` is optional field for moderation - get_llm_provider will throw BadRequestError if model is not set / not recognized
pass
# update litellm_logging_obj with environment variables
custom_llm_provider = custom_llm_provider or litellm.LlmProviders.OPENAI.value
if litellm_logging_obj is not None:
litellm_logging_obj.update_environment_variables(
model=model,
user=kwargs.get("user", None),
optional_params={},
litellm_params={
**kwargs,
},
custom_llm_provider=custom_llm_provider,
)
if model is not None:
response = await _openai_client.moderations.create(input=input, model=model)
else:

View file

@ -41,7 +41,12 @@ class TestCustomLogger(CustomLogger):
pass
@pytest.mark.asyncio
async def test_moderations_api_logging():
@pytest.mark.parametrize("model", [
None,
"omni-moderation-latest"
])
async def test_moderations_api_logging(model):
"""
When moderations API is called, it should log the event on standard_logging_payload
"""
@ -51,6 +56,7 @@ async def test_moderations_api_logging():
response = await litellm.amoderation(
input="Hello, how are you?",
model=model,
)
print("response", json.dumps(response, indent=4, default=str))
@ -58,3 +64,14 @@ async def test_moderations_api_logging():
await asyncio.sleep(2)
assert custom_logger.standard_logging_payload is not None
# validate the standard_logging_payload
standard_logging_payload: StandardLoggingPayload = custom_logger.standard_logging_payload
assert standard_logging_payload["call_type"] == litellm.utils.CallTypes.amoderation.value
assert standard_logging_payload["status"] == "success"
assert standard_logging_payload["custom_llm_provider"] == litellm.LlmProviders.OPENAI.value
# assert the logged response == response user received client side
assert dict(standard_logging_payload["response"]) == response.model_dump()