-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support instructions-only agent run with OpenAIResponsesModel
#3470
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
Changes from 2 commits
72954bb
5bb48f1
2bd352e
9e3bc69
54b6361
eabb3a8
789bd3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1223,11 +1223,22 @@ async def _responses_create( | |
| if model_settings.get('openai_include_web_search_sources'): | ||
| include.append('web_search_call.action.sources') | ||
|
|
||
| # When there are no input messages and we're not reusing a previous response, | ||
| # the OpenAI API will reject a request without any input. To avoid this provide | ||
| # an explicit empty user message. | ||
| if not openai_messages and not previous_response_id: | ||
| openai_messages = [ | ||
|
||
| responses.EasyInputMessageParam( | ||
| role='user', | ||
| content='', | ||
| ) | ||
| ] | ||
|
|
||
| try: | ||
| extra_headers = model_settings.get('extra_headers', {}) | ||
| extra_headers.setdefault('User-Agent', get_user_agent()) | ||
| return await self.client.responses.create( | ||
| input=openai_messages, | ||
| input=cast(responses.ResponseInputParam, openai_messages), | ||
| model=self._model_name, | ||
| instructions=instructions, | ||
| parallel_tool_calls=model_settings.get('parallel_tool_calls', OMIT), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7442,3 +7442,52 @@ def get_meaning_of_life() -> int: | |
| }, | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| async def test_openai_responses_runs_with_deps_only_and_sends_input( | ||
|
||
| allow_model_requests: None, | ||
| ): | ||
| from pydantic import BaseModel | ||
|
|
||
| c = response_message( | ||
| [ | ||
| ResponseOutputMessage( | ||
| id='output-1', | ||
| content=[ResponseOutputText(text='ok', type='output_text', annotations=[])], | ||
| role='assistant', | ||
| status='completed', | ||
| type='message', | ||
| ) | ||
| ] | ||
| ) | ||
|
|
||
| mock_client = MockOpenAIResponses.create_mock(c) | ||
| model = OpenAIResponsesModel('gpt-4o', provider=OpenAIProvider(openai_client=mock_client)) | ||
|
|
||
| class Payload(BaseModel): | ||
| topic: str | ||
| sentences: int | ||
|
|
||
| payload = Payload(topic='artificial intelligence', sentences=3) | ||
|
|
||
| agent = Agent(model=model, instructions='Generate an article.', deps_type=Payload) | ||
|
|
||
| @agent.instructions | ||
| def instr(ctx: Any) -> str: | ||
| # no explicit input passed to run(); this uses ctx.deps | ||
| return f'Topic: {ctx.deps.topic}\nSentences: {ctx.deps.sentences}' | ||
|
|
||
| # Run with only deps | ||
| result = await agent.run(deps=payload) | ||
| assert result.output == 'ok' | ||
|
|
||
| response_kwargs = get_mock_responses_kwargs(mock_client) | ||
| assert response_kwargs, 'Responses API was not called' | ||
|
|
||
| kw = response_kwargs[0] | ||
| assert 'input' in kw or 'text' in kw, "Expected 'input' or 'text' in responses.create kwargs" | ||
|
|
||
| if 'input' in kw: | ||
| assert kw['input'], "Responses 'input' should not be empty when deps are provided" | ||
| else: | ||
| assert kw['text'], "Responses 'text' config should be set when no input messages were built" | ||
Uh oh!
There was an error while loading. Please reload this page.