Repository navigation
fix(gpt_transformation): avoid mutating input messages in remove_cache_control - #41776
vineethsaivs wants to merge 1 commit into
Conversation
|
| ChatCompletionMessageCustomToolCall, | ||
| ChatCompletionMessageToolCall, | ||
| Choices, | ||
| Function, | ||
| Message, | ||
| ModelResponse, | ||
| ModelResponseStream, | ||
| chat_completion_tool_call_from_dict, |
There was a problem hiding this comment.
Unavailable imports break loading
This module now imports ChatCompletionMessageCustomToolCall and chat_completion_tool_call_from_dict, but neither name exists or is imported in litellm.types.utils. Importing gpt_transformation therefore raises ImportError, preventing OpenAI chat support and its tests from loading.
| if custom_llm_provider != "openai": | ||
| return False | ||
| resolved_api_base = api_base or litellm.api_base or os.getenv("OPENAI_BASE_URL") or os.getenv("OPENAI_API_BASE") | ||
| if not resolved_api_base: | ||
| return False | ||
| hostname: Final = urlparse(resolved_api_base).hostname | ||
| if hostname is None: | ||
| return False | ||
| return hostname != "openai.com" and not hostname.endswith(".openai.com") |
There was a problem hiding this comment.
Custom endpoints receive unsupported fields
This check treats every non-OpenAI hostname using the generic openai provider as cache-control capable. If a custom OpenAI-compatible server does not support Anthropic cache_control, it will now receive those fields and may reject requests that previously succeeded after filtering. This violates the repository directive to avoid backward-incompatible behavior without a user-controlled flag.
Rule Used: What: avoid backwards-incompatible changes without user-controlled flags Why: This breaks current behaviour for users using existing functionality Example of BAD: this PR (#22164) introduced run_post_custom... (source)
| @staticmethod | ||
| def _extract_error_from_chunk(chunk: dict) -> tuple[str, int] | None: | ||
| """OpenAI-compatible backends (vLLM, sglang) can return an HTTP 200 | ||
| stream whose body carries an error payload, e.g. | ||
| ``data: {"error": {"message": "...", "code": 400}}``.""" | ||
| error: Final = chunk.get("error") | ||
| if not error: | ||
| return None | ||
| if not isinstance(error, dict): | ||
| return str(error), 500 | ||
| message: Final = error.get("message") | ||
| code: Final = error.get("code") | ||
| status_code: Final = code if isinstance(code, int) and 400 <= code < 600 else 500 | ||
| return (message if isinstance(message, str) else json.dumps(error)), status_code | ||
|
|
||
| def chunk_parser(self, chunk: dict) -> ModelResponseStream: | ||
| try: | ||
| error_details: Final = self._extract_error_from_chunk(chunk) | ||
| if error_details is not None: | ||
| error_message, error_status_code = error_details | ||
| raise OpenAIError( | ||
| status_code=error_status_code, | ||
| message=error_message, | ||
| ) |
There was a problem hiding this comment.
Unrelated behavior expands scope
This mutation fix also introduces streaming error handling, endpoint capability policy, and custom tool-call conversion. The repository directive requires the minimum code needed for the stated problem and prohibits unrequested features. These unrelated behavioral changes must be separated before merging so they can be reviewed and released independently.
Context Used: CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Closing as a duplicate of my #35602, which fixes the same cache_control mutation and is now rebased onto main. |
TLDR
Problem this solves:
How it solves it:
Pre-Submission checklist
Type
🐛 Bug Fix