Skip to content

Fix #980 Chat Completions: fails with function name for tool_choice parameter w/ streaming enabled #1206

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 3 commits into
base: main
Choose a base branch
from

Conversation

seratch
Copy link
Member

@seratch seratch commented Jul 22, 2025

This pull request resolves #980

simple code snippet to do tests:

import asyncio
from openai.types.responses import ResponseTextDeltaEvent
from agents import Agent, ModelSettings, Runner, function_tool, set_default_openai_api

set_default_openai_api("chat_completions")

@function_tool
def say_hello():
    print("Hello, world!")
    return "Hello, world!"

async def main():
    agent = Agent(
        name="Joker",
        instructions="You are a helpful assistant.",
        model_settings=ModelSettings(tool_choice="say_hello"),
        tools=[say_hello],
    )
    result = Runner.run_streamed(agent, input="Please tell me 5 jokes.")
    async for event in result.stream_events():
        if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
            print(event.data.delta, end="", flush=True)

if __name__ == "__main__":
    asyncio.run(main())

responses_tool_choice = OpenAIResponsesConverter.convert_tool_choice(
model_settings.tool_choice
)
if responses_tool_choice is None or responses_tool_choice == NOT_GIVEN:
Copy link
Member Author

@seratch seratch Jul 23, 2025

Choose a reason for hiding this comment

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

The essential changes here are 1) use Converter.convert_tool_choice 2) cover responses_tool_choice is None pattern. Added more detailed comments about why it's necessary but happy to adjust the style if necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Chat Completions: fails with function name for tool_choice parameter w/ streaming enabled
1 participant