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

Code before:

        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 first_error:
                fallback_kwargs = dict(template_kwargs)
                tools = fallback_kwargs.get("tools")
                # Try an alternative tools payload shape used by some templates.
                if not tools:
                    logger.warning(
                        "SGLang chat template rendering failed; falling back to "
                        "simple role/content prompt. first_error=%s",
                        first_error,
                    )
                    return self._render_fallback_prompt(messages)

                fallback_kwargs["tools"] = [
                    item
                    if isinstance(item, dict) and "function" in item
                    else {"function": item}
                    for item in tools
                ]
                try:
                    return tokenizer.apply_chat_template(
                        messages,
                        tokenize=False,
                        add_generation_prompt=True,
                        **fallback_kwargs,
                    )
                except Exception as second_error:

Review comment (suggestion):
Could you share some background/justification for the fallback/exception handling stack here or remove if not needed

Code after:
        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.
        return

    async def check_health(self) -> None:
        # SGLang's in-process Engine API does not expose a health-check method.
        # Its health endpoints exist only in HTTP/gRPC server entrypoints, which
        # this integration does not run. Keep the protocol hook as a no-op.
        return

    async def _generate_raw(
        self,
        request: Any,