Skip to content

fallback for json.loads tool function args #1164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thermokarst
Copy link

some openai-compatible models (such as openrouter's anthropic models) do not default to an empty json object when there are no arguments, leading to a parsing error.

function.arguments == '{}':

% llm --tool llm_version "What is the version?" --td -m openrouter/openai/gpt-4o

Tool call: llm_version({})
  0.26

The version of the LLM is 0.26.

function.arguments == '':

% llm --tool llm_version "What is the version?" --td -m openrouter/anthropic/claude-sonnet-4
I'll check the version of llm for you.Error: Expecting value: line 1 column 1 (char 0)

note - the examples above are using simonw/llm-openrouter#39

@kanaka
Copy link

kanaka commented Jul 2, 2025

@thermokarst I ran into the same issue and created a PR for it: #1170 (didn't recognize yours as the same issue at the time). I'm not sure if it ever happens in practice, but I think you need to account for the possibility arguments split between streamed chunks. I'm happy to close mine and point to yours (since it's earlier) if you account for that possibility (or can convince me that it would never happen).

some openai-compatible models (such as openrouter's anthropic models)
do not default to an empty json object when there are no arguments,
leading to a parsing error.

`function.arguments == '{}'`:

```bash
% llm --tool llm_version "What is the version?" --td -m openrouter/openai/gpt-4o

Tool call: llm_version({})
  0.26

The version of the LLM is 0.26.
```

`function.arguments == ''`:

```bash
% llm --tool llm_version "What is the version?" --td -m openrouter/anthropic/claude-sonnet-4
I'll check the version of llm for you.Error: Expecting value: line 1 column 1 (char 0)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants