diff --git a/src/pipecat/services/openai/base_llm.py b/src/pipecat/services/openai/base_llm.py index 86ed6bdbc..3fa9ae216 100644 --- a/src/pipecat/services/openai/base_llm.py +++ b/src/pipecat/services/openai/base_llm.py @@ -133,6 +133,7 @@ class BaseOpenAILLMService(LLMService): self._retry_timeout_secs = retry_timeout_secs self._retry_on_timeout = retry_on_timeout self.set_model_name(model) + self._full_model_name: str = "" self._client = self.create_client( api_key=api_key, base_url=base_url, @@ -185,6 +186,22 @@ class BaseOpenAILLMService(LLMService): """ return True + def set_full_model_name(self, full_model_name: str): + """Set the full AI model name. + + Args: + full_model_name: The full name of the AI model to use. + """ + self._full_model_name = full_model_name + + def get_full_model_name(self): + """Get the current full model name. + + Returns: + The full name of the AI model being used. + """ + return self._full_model_name + async def get_chat_completions( self, params_from_context: OpenAILLMInvocationParams ) -> AsyncStream[ChatCompletionChunk]: @@ -360,6 +377,9 @@ class BaseOpenAILLMService(LLMService): ) await self.start_llm_usage_metrics(tokens) + if chunk.model and self.get_full_model_name() != chunk.model: + self.set_full_model_name(chunk.model) + if chunk.choices is None or len(chunk.choices) == 0: continue diff --git a/src/pipecat/utils/tracing/service_decorators.py b/src/pipecat/utils/tracing/service_decorators.py index 7bcb3b63e..af9985d54 100644 --- a/src/pipecat/utils/tracing/service_decorators.py +++ b/src/pipecat/utils/tracing/service_decorators.py @@ -483,7 +483,9 @@ def traced_llm(func: Optional[Callable] = None, *, name: Optional[str] = None) - # Add all available attributes to the span attribute_kwargs = { "service_name": service_class_name, - "model": getattr(self, "model_name", "unknown"), + "model": getattr( + self, getattr(self, "_full_model_name", "model_name"), "unknown" + ), "stream": True, # Most LLM services use streaming "parameters": params, }