Skip to content
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

add user mapping in openai chat completion #1174

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ovisek
Copy link

@ovisek ovisek commented Mar 19, 2025

Added the fix as documented in issue :
#1156

This pull request includes a small change to the pydantic_ai_slim/pydantic_ai/models/openai.py file. The change adds a new parameter to the _completions_create method to include the user setting from model_settings.

Following the changes we can pass on the user field as part of model setting and pass the model setting in the agent

Example :

openai_model_settings = OpenAIModelSettings()
openai_model_settings["user"] = current_user

result = agent.run_sync(user_prompt='<USER PROMPT>',
                        model_settings=openai_model_settings,)

@ovisek
Copy link
Author

ovisek commented Mar 19, 2025

make output

uv run ruff format
1 file reformatted, 131 files left unchanged
uv run ruff check --fix --fix-only
uv run ruff format --check
132 files already formatted
uv run ruff check
All checks passed!
PYRIGHT_PYTHON_IGNORE_WARNINGS=1 uv run pyright

added 1 package, and audited 2 packages in 6s

found 0 vulnerabilities
npm notice
npm notice New major version of npm available! 10.9.2 -> 11.2.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.2.0
npm notice To update run: npm install -g [email protected]
npm notice
0 errors, 0 warnings, 0 informations 
uv run coverage run -m pytest
==================================================================================== test session starts ====================================================================================
platform darwin -- Python 3.12.9, pytest-8.3.4, pluggy-1.5.0
rootdir: /Users/<user>/Workspace/_personal/opensource/forks/pydantic-ai
configfile: pyproject.toml
testpaths: tests
plugins: recording-0.13.2, devtools-0.12.2, logfire-3.6.1, anyio-4.8.0, examples-0.0.15, inline-snapshot-0.20.2, mock-3.14.0, pretty-1.2.0
collected 709 items                                                                                                                                                                         

tests/graph/test_file_persistence.py .....                                                                                                                                            [  0%]
tests/graph/test_graph.py .............                                                                                                                                               [  2%]
tests/graph/test_mermaid.py .........................                                                                                                                                 [  6%]
tests/graph/test_persistence.py .............                                                                                                                                         [  7%]
tests/graph/test_state.py .                                                                                                                                                           [  8%]
tests/graph/test_utils.py .                                                                                                                                                           [  8%]
tests/models/test_anthropic.py ....................                                                                                                                                   [ 11%]
tests/models/test_bedrock.py ..............                                                                                                                                           [ 12%]
tests/models/test_cohere.py .......                                                                                                                                                   [ 13%]
tests/models/test_fallback.py ...........                                                                                                                                             [ 15%]
tests/models/test_gemini.py ............................                                                                                                                              [ 19%]
tests/models/test_groq.py ..................                                                                                                                                          [ 22%]
tests/models/test_instrumented.py ......                                                                                                                                              [ 22%]
tests/models/test_mistral.py .................................                                                                                                                        [ 27%]
tests/models/test_model.py ..............                                                                                                                                             [ 29%]
tests/models/test_model_function.py ................                                                                                                                                  [ 31%]
tests/models/test_model_names.py .                                                                                                                                                    [ 31%]
tests/models/test_model_test.py ................                                                                                                                                      [ 34%]
tests/models/test_openai.py ...........................                                                                                                                               [ 37%]
tests/models/test_vertexai.py .....                                                                                                                                                   [ 38%]
tests/providers/test_anthropic.py .....                                                                                                                                               [ 39%]
tests/providers/test_azure.py ....                                                                                                                                                    [ 39%]
tests/providers/test_bedrock.py .                                                                                                                                                     [ 40%]
tests/providers/test_deepseek.py ....                                                                                                                                                 [ 40%]
tests/providers/test_google_gla.py .                                                                                                                                                  [ 40%]
tests/providers/test_google_vertex.py .....                                                                                                                                           [ 41%]
tests/providers/test_groq.py .....                                                                                                                                                    [ 42%]
tests/providers/test_mistral.py .....                                                                                                                                                 [ 42%]
tests/providers/test_provider_names.py .......                                                                                                                                        [ 43%]
tests/test_agent.py .................................................                                                                                                                 [ 50%]
tests/test_cli.py ...                                                                                                                                                                 [ 51%]
tests/test_deps.py ..                                                                                                                                                                 [ 51%]
tests/test_examples.py ssss............s....................s................................................................ss................s..................................... [ 73%]
..................                                                                                                                                                                    [ 76%]
tests/test_format_as_xml.py .......................                                                                                                                                   [ 79%]
tests/test_json_body_serializer.py ......                                                                                                                                             [ 80%]
tests/test_live.py ssssssssssssssssssss                                                                                                                                               [ 83%]
tests/test_logfire.py .....                                                                                                                                                           [ 83%]
tests/test_mcp.py ....                                                                                                                                                                [ 84%]
tests/test_messages.py ................                                                                                                                                               [ 86%]
tests/test_parts_manager.py .......................                                                                                                                                   [ 89%]
tests/test_streaming.py ..................                                                                                                                                            [ 92%]
tests/test_tools.py ....................................                                                                                                                              [ 97%]
tests/test_usage_limits.py ........                                                                                                                                                   [ 98%]
tests/test_utils.py .........                                                                                                                                                         [100%]
====================================================================================== inline snapshot ======================================================================================
Info: one snapshot changed its representation (--inline-snapshot=update)

You can also use --inline-snapshot=review to approve the changes interactively
Results (74.81s):
       680 passed
        29 skipped
Name                                                      Stmts   Miss Branch BrPart   Cover   Missing
------------------------------------------------------------------------------------------------------
pydantic_ai_slim/pydantic_ai/_agent_graph.py                353      3    108      7  97.83%   191->188, 260, 281, 311, 327->331, 348->352, 693->691
pydantic_ai_slim/pydantic_ai/_parts_manager.py               87      0     38      1  99.20%   165->176
pydantic_ai_slim/pydantic_ai/_result.py                     144      3     56      7  95.00%   122->exit, 123->122, 133->132, 176, 217, 243->exit, 247
pydantic_ai_slim/pydantic_ai/agent.py                       366      7    122      6  97.34%   701, 748, 752, 861->863, 1218->exit, 1395, 1500-1502
pydantic_ai_slim/pydantic_ai/exceptions.py                   62      1      6      1  97.06%   7
pydantic_ai_slim/pydantic_ai/messages.py                    385     14    110     10  94.34%   55-60, 79, 81, 83, 189, 213, 226, 288, 528, 595
pydantic_ai_slim/pydantic_ai/models/__init__.py             107      1     32      1  98.56%   430
pydantic_ai_slim/pydantic_ai/models/anthropic.py            219     10     92     13  92.60%   165-167, 169, 274, 298, 335->321, 337, 377, 442, 501->494, 508->494, 513, 514->494, 533->494
pydantic_ai_slim/pydantic_ai/models/bedrock.py              214      4     78      9  95.55%   196->210, 249, 285, 384->361, 430->432, 436->448, 447, 461->423, 499
pydantic_ai_slim/pydantic_ai/models/cohere.py               127      4     46      7  93.64%   178, 189->188, 268, 286->288, 288->290, 291, 293
pydantic_ai_slim/pydantic_ai/models/fallback.py              68      1     14      2  96.34%   99, 112->exit
pydantic_ai_slim/pydantic_ai/models/gemini.py               435     15    162     24  93.13%   147->152, 216, 250->252, 278, 283, 288, 304->298, 334, 341->321, 392, 408, 427->410, 430, 458->exit, 612-613, 642->653, 646, 648, 698->700, 762, 765, 838->846, 839->846, 840->839, 862
pydantic_ai_slim/pydantic_ai/models/groq.py                 186      8     70      7  94.14%   144-146, 148, 251, 270, 341->330, 343, 428
pydantic_ai_slim/pydantic_ai/models/instrumented.py         129      1     42      5  96.49%   135->exit, 222->224, 224->227, 232, 248->250
pydantic_ai_slim/pydantic_ai/models/mistral.py              305     15    124     10  94.17%   75-76, 169-171, 242, 366, 465-466, 483, 531-532, 674, 688, 689->693, 694
pydantic_ai_slim/pydantic_ai/models/openai.py               196      3     72      3  97.76%   296, 315, 393
pydantic_ai_slim/pydantic_ai/models/test.py                 258      1    122      2  99.21%   153, 172->200
pydantic_ai_slim/pydantic_ai/models/vertexai.py             101      5     12      0  95.58%   158-159, 168-170
pydantic_ai_slim/pydantic_ai/models/wrapper.py               28      1      0      0  96.43%   45
pydantic_ai_slim/pydantic_ai/providers/google_vertex.py      89      2     16      5  93.33%   137->139, 139->142, 163->167, 165, 173
pydantic_ai_slim/pydantic_ai/providers/openai.py             28      1      6      1  94.12%   67
pydantic_ai_slim/pydantic_ai/result.py                      215     17     90     12  89.18%   72->exit, 82-84, 105, 146->exit, 150->exit, 231, 249, 263, 370, 378, 383, 393, 435, 436->429, 480-485
pydantic_ai_slim/pydantic_ai/tools.py                       157      4     26      4  95.63%   69->71, 150->152, 297-298, 335, 349
pydantic_graph/pydantic_graph/_utils.py                      50      1     16      3  93.94%   26, 64->exit, 65->exit
pydantic_graph/pydantic_graph/graph.py                      227      2    106      8  97.00%   25-26, 202->205, 259->261, 300->303, 304->306, 363->366, 528->531, 581->exit, 583->exit
pydantic_graph/pydantic_graph/nodes.py                      110      1     20      2  97.69%   97, 98->103
pydantic_graph/pydantic_graph/persistence/__init__.py        66      1      6      0  98.61%   225
pydantic_graph/pydantic_graph/persistence/file.py           108      0     10      1  99.15%   62->exit
pydantic_graph/pydantic_graph/persistence/in_mem.py         100      1     12      1  98.21%   48
tests/conftest.py                                           140      2      4      1  97.92%   43->45, 165-166
tests/graph/test_file_persistence.py                         85      2      2      1  96.55%   192, 204
tests/graph/test_graph.py                                   214      1      4      0  99.54%   411
tests/graph/test_persistence.py                             135      2      0      0  98.52%   329, 342
tests/json_body_serializer.py                                50      7     24      4  82.43%   13-14, 64->76, 71->76, 74, 77-80
tests/models/test_anthropic.py                              228      2     10      2  98.32%   112, 339
tests/models/test_fallback.py                               117      2      4      1  97.52%   19, 350
tests/models/test_gemini.py                                 343      1     12      1  99.44%   706, 911->913
tests/models/test_groq.py                                   211      1      8      1  99.09%   98
tests/models/test_instrumented.py                            94      0      2      1  98.96%   442->446
tests/models/test_model.py                                   23      2      0      0  91.30%   101-102
tests/models/test_model_function.py                         194      1     10      0  99.51%   490
tests/models/test_openai.py                                 234      1      2      0  99.58%   529
tests/test_agent.py                                         489      3     20      1  99.21%   367-368, 1271
tests/test_examples.py                                      157     11     46      3  92.12%   40-41, 46-47, 81->85, 102, 184-187, 455-456
tests/test_logfire.py                                        85      2      8      0  97.85%   17-18
tests/test_streaming.py                                     286      1     24      1  99.35%   139
tests/test_tools.py                                         302      0     20      2  99.38%   507->exit, 518->exit
tests/test_utils.py                                          53      5      8      1  86.89%   102-106
------------------------------------------------------------------------------------------------------
TOTAL                                                     11272    173   2142    172  97.31%

54 files skipped due to complete coverage.
building coverage html
Wrote HTML report to htmlcov/index.html

@Kludex
Copy link
Member

Kludex commented Mar 19, 2025

@ovisek
Copy link
Author

ovisek commented Mar 19, 2025

@Kludex I think you intended to reference #1042 . This is solving the same issue but here the approach is to bind this parameter to OpenAIModelSetting rather than having it as a parameter to initialize the model. Let me know if we are good with this solution

@Kludex
Copy link
Member

Kludex commented Mar 19, 2025

@Kludex I think you intended to reference #1042 . This is solving the same issue but here the approach is to bind this parameter to OpenAIModelSetting rather than having it as a parameter to initialize the model. Let me know if we are good with this solution

Why should it be here instead of there? For reference.

@ovisek
Copy link
Author

ovisek commented Mar 19, 2025

@Kludex I think you intended to reference #1042 . This is solving the same issue but here the approach is to bind this parameter to OpenAIModelSetting rather than having it as a parameter to initialize the model. Let me know if we are good with this solution

Why should it be here instead of there? For reference.

From the OpenAI documentation : user parameter is passed as a parameter during chat completion call

https://platform.openai.com/docs/api-reference/chat/create

Also if you refer the openai-python library https://github.com/openai/openai-python/blob/main/src/openai/types/chat/completion_create_params.py
these parameters are part of the model settings.

It makes sense to make it part of OpenAIModelSetting rather than the OpenAIModel itself

@ovisek
Copy link
Author

ovisek commented Mar 23, 2025

@Kludex Could you get a chance to review this?

@Kludex Kludex enabled auto-merge (squash) March 25, 2025 12:46
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