How do i fix when tool calls filter thru into agen...
# questions
b
How do i fix when tool calls filter thru into agent ouput - this is w/ kimi
<|tool_calls_section_begin|><|tool_call_begin|>functions.execute_ipython_cell:1<|tool_call_argument_begin|>{"code": "# Final question: predict employment for 1965 based on 1953-1963 trend\n# We need to create a trend line based on the early years\n\n# Get data from 1953 to 1963 (the period for learning the trend)\nlearning_years = []\nlearning_emp_rates = []\n\nfor i in range(len(years)):\n if 1953 <= years[i] <= 1963:\n learning_years.append(years[i])\n learning_emp_rates.append(employed_percentages[i])\n\nprint("Data for trend calculation (1953-1963):")\nprint(f"Years: {learning_years}")\nprint(f"Employment rates: {learning_emp_rates}")\n\n# Calculate linear regression (trend)\nn = len(learning_years)\nsum_x = sum(learning_years)\nsum_y = sum(learning_emp_rates)\nsum_xy = sum(x*y for x,y in zip(learning_years, learning_emp_rates))\nsum_x2 = sum(x*2 for x in learning_years)\n\n# Slope (m) and intercept (b) of the trend line\nslope = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x*2)\nintercept = (sum_y - slope * sum_x) / n\n\nprint(f"\nTrend equation: y = {slope:.4f}x + {intercept:.4f}")\n\n# Predict for 1965\npredicted_1965 = slope * 1965 + intercept\nprint(f"Predicted employment rate for 1965: {predicted_1965:.1f}%")\n\n# Compare with actual value\nactual_1965 = employed_percentages[years.index(1965)]\nprint(f"Actual employment rate for 1965: {actual_1965}%")\nprint(f"Difference: {abs(predicted_1965 - actual_1965):.1f} percentage points")"}<|tool_call_end|><|tool_calls_section_end|>
l
Hmm, this looks like it might be a litellm issue? @nutritious-balloon-65098 examine the berriai/litellm repo and online documentation for kimi-k2 and see if there are any possible issues here?
n
I'm on it! Graham Neubig can track my progress here.
Summary I've completed a thorough investigation of the kimi-k2 tool calls filtering issue. Here's what I found: The Issue is Confirmed: This is a LiteLLM Problem Root Cause: • Kimi-K2 models output tool calls in a special native format with tokens like <>, <>, etc. • LiteLLM's
MoonshotChatConfig
class treats Kimi/Moonshot as OpenAI-compatible but lacks response transformation logic to parse or strip these native tokens • The class only has request transformation (for temperature, tool_choice, etc.) but doesn't override the
transform_response()
method • This causes raw tokens to leak through into the final agent output Evidence: 1. ✅ Examined LiteLLM source code - confirmed no response parsing for Kimi-K2's format 2. ✅ Found official Kimi-K2 documentation describing the special token format 3. ✅ Discovered this is a known issue - documented in Hugging Face Discussion #41 Solutions Provided: I've created a comprehensive analysis document at
/workspace/project/kimi-k2-tool-calls-issue-analysis.md
with: 1. Immediate Workaround - Regex-based post-processing to strip tokens from output:
Copy code
def strip_kimi_tool_tokens(text: str) -> str:
    pattern = r'<\>.*?<\>'
    text = re.sub(pattern, '', text, flags=re.DOTALL)
    return text.strip()
2. Long-term Fix - Guidance on how LiteLLM should add response transformation in
MoonshotChatConfig
3. Alternative - Using vLLM with proper tool-call parser:
--enable-auto-tool-choice --tool-call-parser kimi_k2
The document includes code examples, links to relevant documentation, and manual parsing logic from the official Kimi-K2 docs. Bottom Line: LiteLLM needs to add response post-processing for Kimi-K2 models. Until then, use the workaround to clean the output in your application layer. <sub>View full conversation</sub>
l
So @gorgeous-horse-69467 I think this might be a litellm problem, you could potentially raise it with them
b
it's kind of a bummer cause i tried kimi to their API and to Groq and it does not work native ^^^ but does on Groq. Same time they claim much better tool calling performance on their endpoint.... https://github.com/MoonshotAI/K2-Vendor-Verifier
l
Yeah, I think if there’s a way to get litellm to properly parse this (and I think there is) then the problem would be fixed
b
they dont have moonshot native provider .....
separately - if i am running gpt5nano and get 'Missing required parameters for function 'execute_ipython_cell': {'security_risk'}" a lot - any tips for how to improve it?
b
Curious if you guys ever came to a resolution on this @breezy-author-40257 @limited-student-10747? I'm also experiencing some issues with native tool calling and kimi k2.
Also @breezy-author-40257 - I see this error a ton too. My hypothesis is that when not using native tool calling, the schema for the tools is much less strictly enforced. That means its more likely that models skip required parameters like security risk in their output.
l
We haven’t had time to look specifically at kimi-k2 yet unfortunately
b
No worries, when you last left off did you just stick with non-native tool calling to avoid this?