|
| 1 | +from typing import TYPE_CHECKING, Any, Dict, List, Optional |
| 2 | + |
| 3 | +import litellm |
| 4 | +from litellm._logging import verbose_logger |
| 5 | +from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig |
| 6 | +from litellm.secret_managers.main import get_secret_str |
| 7 | +from litellm.types.llms.openai import ResponsesAPIOptionalRequestParams |
| 8 | +from litellm.types.router import GenericLiteLLMParams |
| 9 | +from litellm.types.utils import LlmProviders |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj |
| 13 | + |
| 14 | + LiteLLMLoggingObj = _LiteLLMLoggingObj |
| 15 | +else: |
| 16 | + LiteLLMLoggingObj = Any |
| 17 | + |
| 18 | +XAI_API_BASE = "https://api.x.ai/v1" |
| 19 | + |
| 20 | + |
| 21 | +class XAIResponsesAPIConfig(OpenAIResponsesAPIConfig): |
| 22 | + """ |
| 23 | + Configuration for XAI's Responses API. |
| 24 | + |
| 25 | + Inherits from OpenAIResponsesAPIConfig since XAI's Responses API is largely |
| 26 | + compatible with OpenAI's, with a few differences: |
| 27 | + - Does not support the 'instructions' parameter |
| 28 | + - Requires code_interpreter tools to have 'container' field removed |
| 29 | + - Recommends store=false when sending images |
| 30 | + |
| 31 | + Reference: https://docs.x.ai/docs/api-reference#create-new-response |
| 32 | + """ |
| 33 | + |
| 34 | + @property |
| 35 | + def custom_llm_provider(self) -> LlmProviders: |
| 36 | + return LlmProviders.XAI |
| 37 | + |
| 38 | + def get_supported_openai_params(self, model: str) -> list: |
| 39 | + """ |
| 40 | + Get supported parameters for XAI Responses API. |
| 41 | + |
| 42 | + XAI supports most OpenAI Responses API params except 'instructions'. |
| 43 | + """ |
| 44 | + supported_params = super().get_supported_openai_params(model) |
| 45 | + |
| 46 | + # Remove 'instructions' as it's not supported by XAI |
| 47 | + if "instructions" in supported_params: |
| 48 | + supported_params.remove("instructions") |
| 49 | + |
| 50 | + return supported_params |
| 51 | + |
| 52 | + def map_openai_params( |
| 53 | + self, |
| 54 | + response_api_optional_params: ResponsesAPIOptionalRequestParams, |
| 55 | + model: str, |
| 56 | + drop_params: bool, |
| 57 | + ) -> Dict: |
| 58 | + """ |
| 59 | + Map parameters for XAI Responses API. |
| 60 | + |
| 61 | + Handles XAI-specific transformations: |
| 62 | + 1. Drops 'instructions' parameter (not supported) |
| 63 | + 2. Transforms code_interpreter tools to remove 'container' field |
| 64 | + 3. Sets store=false when images are detected (recommended by XAI) |
| 65 | + """ |
| 66 | + params = dict(response_api_optional_params) |
| 67 | + |
| 68 | + # Drop instructions parameter (not supported by XAI) |
| 69 | + if "instructions" in params: |
| 70 | + verbose_logger.debug( |
| 71 | + "XAI Responses API does not support 'instructions' parameter. Dropping it." |
| 72 | + ) |
| 73 | + params.pop("instructions") |
| 74 | + |
| 75 | + # Transform code_interpreter tools - remove container field |
| 76 | + if "tools" in params and params["tools"]: |
| 77 | + tools_list = params["tools"] |
| 78 | + # Ensure tools is a list for iteration |
| 79 | + if not isinstance(tools_list, list): |
| 80 | + tools_list = [tools_list] |
| 81 | + |
| 82 | + transformed_tools: List[Any] = [] |
| 83 | + for tool in tools_list: |
| 84 | + if isinstance(tool, dict) and tool.get("type") == "code_interpreter": |
| 85 | + # XAI supports code_interpreter but doesn't use the container field |
| 86 | + # Keep only the type field |
| 87 | + verbose_logger.debug( |
| 88 | + "XAI: Transforming code_interpreter tool, removing container field" |
| 89 | + ) |
| 90 | + transformed_tools.append({"type": "code_interpreter"}) |
| 91 | + else: |
| 92 | + transformed_tools.append(tool) |
| 93 | + params["tools"] = transformed_tools |
| 94 | + |
| 95 | + return params |
| 96 | + |
| 97 | + def validate_environment( |
| 98 | + self, headers: dict, model: str, litellm_params: Optional[GenericLiteLLMParams] |
| 99 | + ) -> dict: |
| 100 | + """ |
| 101 | + Validate environment and set up headers for XAI API. |
| 102 | + |
| 103 | + Uses XAI_API_KEY from environment or litellm_params. |
| 104 | + """ |
| 105 | + litellm_params = litellm_params or GenericLiteLLMParams() |
| 106 | + api_key = ( |
| 107 | + litellm_params.api_key |
| 108 | + or litellm.api_key |
| 109 | + or get_secret_str("XAI_API_KEY") |
| 110 | + ) |
| 111 | + |
| 112 | + if not api_key: |
| 113 | + raise ValueError( |
| 114 | + "XAI API key is required. Set XAI_API_KEY environment variable or pass api_key parameter." |
| 115 | + ) |
| 116 | + |
| 117 | + headers.update( |
| 118 | + { |
| 119 | + "Authorization": f"Bearer {api_key}", |
| 120 | + } |
| 121 | + ) |
| 122 | + return headers |
| 123 | + |
| 124 | + def get_complete_url( |
| 125 | + self, |
| 126 | + api_base: Optional[str], |
| 127 | + litellm_params: dict, |
| 128 | + ) -> str: |
| 129 | + """ |
| 130 | + Get the complete URL for XAI Responses API endpoint. |
| 131 | + |
| 132 | + Returns: |
| 133 | + str: The full URL for the XAI /responses endpoint |
| 134 | + """ |
| 135 | + api_base = ( |
| 136 | + api_base |
| 137 | + or litellm.api_base |
| 138 | + or get_secret_str("XAI_API_BASE") |
| 139 | + or XAI_API_BASE |
| 140 | + ) |
| 141 | + |
| 142 | + # Remove trailing slashes |
| 143 | + api_base = api_base.rstrip("/") |
| 144 | + |
| 145 | + return f"{api_base}/responses" |
| 146 | + |
0 commit comments