Skip to content

[side-effect] The tool message dump is incomplete#4299

Merged
lvhan028 merged 2 commits intoInternLM:mainfrom
lvhan028:fix-side-effect
Jan 28, 2026
Merged

[side-effect] The tool message dump is incomplete#4299
lvhan028 merged 2 commits intoInternLM:mainfrom
lvhan028:fix-side-effect

Conversation

@lvhan028
Copy link
Copy Markdown
Collaborator

Fix side effect brought by #4251

In the following example, the "parameters" part of tools is missed from the dump dict.

from openai import OpenAI
import json

tools = [{
    'type': 'function',
    'function': {
        'name': 'get_current_temperature',
        'description': 'Get current temperature at a location.',
        'parameters': {
            'type': 'object',
            'properties': {
                'location': {
                    'type': 'string',
                    'description': 'The location to get the temperature for, in the format \'City, State, Country\'.'
                },
                'unit': {
                    'type': 'string',
                    'enum': [
                        'celsius',
                        'fahrenheit'
                    ],
                    'description': 'The unit to return the temperature in. Defaults to \'celsius\'.'
                }
            },
            'required': [
                'location'
            ]
        }
    }
},
]



messages = [
    {'role': 'user', 'content': 'Today is 2024-11-14, What\'s the temperature in San Francisco now? How about tomorrow?'}
]

openai_api_key = "EMPTY"
openai_api_base = "http://0.0.0.0:23333/v1"
client = OpenAI(
    api_key=openai_api_key,
    base_url=openai_api_base,
)
model_name = client.models.list().data[0].id
response = client.chat.completions.create(
    model=model_name,
    messages=messages,
    max_tokens=32768,
    temperature=0.8,
    top_p=0.8,
    # stream=False,
    stream=True,
    # extra_body=dict(spaces_between_special_tokens=False, enable_thinking=True),
    extra_body=dict(enable_thinking=True),
    tools=tools
    )
for res in response:
    print(res)

Copilot AI review requested due to automatic review settings January 27, 2026 13:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a regression where OpenAI-compatible tool definitions lose the parameters payload when being dumped/serialized.

Changes:

  • Adjustes the Pydantic type of Function.parameters to allow preserving arbitrary JSON-schema-like objects in dumps.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

description: Optional[str] = Field(default=None, examples=[None])
name: str
parameters: Optional[BaseModel] = None
parameters: Optional[object] = None
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

parameters is typed as object, which is overly permissive and allows non-mapping JSON values (e.g., a string/array) to pass validation even though OpenAI tool parameters is expected to be a JSON Schema object. Consider changing this to Optional[Dict[str, Any]] (or Optional[Mapping[str, Any]]) so request validation and generated OpenAPI schema more accurately reflect the expected shape while still preserving the full dict on model_dump().

Suggested change
parameters: Optional[object] = None
parameters: Optional[Dict[str, Any]] = None

Copilot uses AI. Check for mistakes.
description: Optional[str] = Field(default=None, examples=[None])
name: str
parameters: Optional[BaseModel] = None
parameters: Optional[object] = None
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

This change fixes a regression around tool parameters being dropped during dumps, but there doesn’t appear to be a unit test covering ChatCompletionRequest parsing + Tool/Function.model_dump() retaining nested parameters. Please add a regression test that constructs a request with tools[0].function.parameters set to a nested JSON schema dict and asserts it survives a model_dump()/model_dump_json() round-trip.

Copilot uses AI. Check for mistakes.
@windreamer
Copy link
Copy Markdown
Collaborator

Thank you for fixing my mistakes!

@lvhan028 lvhan028 requested a review from CUHKSZzxy January 28, 2026 02:43
@lvhan028 lvhan028 merged commit ddb5323 into InternLM:main Jan 28, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants