Repository: ray-project/ray
File: python/ray/llm/examples/sglang/modules/sglang_engine.py (Python)

Code before:
        Build optional chat-template kwargs using request fields when present.
        This mirrors SGLang's chat-serving pipeline semantics without directly
        coupling to its internal server classes.
        """
        kwargs: dict[str, Any] = {}

        tools = getattr(request, "tools", None)
        if tools is not None:
            kwargs["tools"] = tools

        reasoning_effort = getattr(request, "reasoning_effort", None)
        if reasoning_effort is not None:
            kwargs["reasoning_effort"] = reasoning_effort

        chat_template_kwargs = getattr(request, "chat_template_kwargs", None)
        if isinstance(chat_template_kwargs, dict):
            kwargs.update(chat_template_kwargs)

        return kwargs

    def _render_chat_prompt(
        self,
        request: ChatCompletionRequest,
        messages: List[dict[str, Any]],
    ) -> str:
        tokenizer = getattr(
            getattr(self.engine, "tokenizer_manager", None), "tokenizer", None
        )

        if tokenizer is not None and hasattr(tokenizer, "apply_chat_template"):
            template_kwargs = self._build_chat_template_kwargs(request)
            try:
                return tokenizer.apply_chat_template(
                    messages,
                    tokenize=False,
                    add_generation_prompt=True,
                    **template_kwargs,
                )
            except Exception as template_error:
                logger.warning(
                    "SGLang chat template rendering failed; falling back to "
                    "simple role/content prompt. error=%s",
                    template_error,
                )

        return self._render_fallback_prompt(messages)

    @staticmethod
    def _render_fallback_prompt(messages: List[dict[str, Any]]) -> str:
        # Fallback prompt format for tokenizers without chat-template support.
        prompt_lines: List[str] = []

Review comment (bug):
Can this `getattr` actually fail? For these `getattr`/`isinstance` checks, please verify that they're actually needed and add comments describing when they might fail.

Code after:
        kwargs: dict[str, Any] = {}

        tools = getattr(request, "tools", None)
        if tools is not None:
            kwargs["tools"] = tools

        reasoning_effort = getattr(request, "reasoning_effort", None)
        if reasoning_effort is not None:
            kwargs["reasoning_effort"] = reasoning_effort

        chat_template_kwargs = getattr(request, "chat_template_kwargs", None)
        if isinstance(chat_template_kwargs, dict):
            kwargs.update(chat_template_kwargs)

        return kwargs

    def _render_chat_prompt(
        self,
        request: ChatCompletionRequest,
        messages: List[dict[str, Any]],
    ) -> str:
        tokenizer = self.engine.tokenizer_manager.tokenizer
        # SGLang supports --skip-tokenizer-init, where tokenizer is intentionally
        # None and text prompt rendering is not available.
        if tokenizer is None:
            return self._render_fallback_prompt(messages)

        template_kwargs = self._build_chat_template_kwargs(request)
        return tokenizer.apply_chat_template(
            messages,
            tokenize=False,
            add_generation_prompt=True,
            **template_kwargs,
        )

    @staticmethod
    def _render_fallback_prompt(messages: List[dict[str, Any]]) -> str:
        # Fallback prompt format for tokenizers without chat-template support.
        prompt_lines: List[str] = []
        for message in messages:
            role = str(message.get("role", "user"))
            content = message.get("content", "")
            if content is None:
                content = ""
            prompt_lines.append(f"{role}: {content}")
        prompt_lines.append("assistant:")
        return "\n".join(prompt_lines)

    async def start(self) -> None:
        # Engine is initialized in __init__; keep start idempotent for protocol
        # compatibility.