Skip to content

Conversation

Arindam200
Copy link

Description

Enhanced structured output handling for tool calls in LiteLLMModel.

  • Updated response parsing to correctly detect and process tool_calls within LiteLLM responses.
  • If no tool_calls are present, the model now parses the message.content directly.
  • Added tests to validate behavior for both tool-call and non-tool-call cases.

This ensures structured outputs are consistently parsed across all providers, preventing missing or malformed data in tool responses.

Related Issues

Fixes #1005

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare
  • Added and verified unit tests for tool-call and non-tool-call scenarios.

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

raise ContextWindowOverflowException(e) from e
except (json.JSONDecodeError, TypeError, ValueError) as e:
raise ValueError(f"Failed to parse or load content into model: {e}") from e
if choice.finish_reason == "tool_calls" and getattr(choice.message, "tool_calls", None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this logic, or at least don't need it now.

In the previous state we looked at just choice.message.content even when tool_calls was the finished reason. So the proposal you have here is to switch to look at the function arguments.

I'd rather make one small change at a time, which I believe is simply removing the check as you did in

response.choices[0].finish_reason != "tool_calls"

but I don't think we need to introduce the logic you have here and can instead just rely on the existing as we had before unless there is a compelling reason to

        try:
            # Parse the message content as JSON
            tool_call_data = json.loads(choice.message.content)
            # Instantiate the output model with the parsed data
            return output_model(**tool_call_data)
        except ContextWindowExceededError as e:
            logger.warning("litellm client raised context window overflow in structured_output")
            raise ContextWindowOverflowException(e) from e
        except (json.JSONDecodeError, TypeError, ValueError) as e:
            raise ValueError(f"Failed to parse or load content into model: {e}") from e

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.

[BUG] : structured_output hard fails when LiteLLM returns JSON without tool_calls finish_reason

2 participants