openai Python 3.51.0: OpenAI(api_key='sk-dummy', http_client=httpx.Client(transport=httpx.MockTransport(handler))) accepted, chat.completions.create returned the mocked response
Passing httpx.Client(transport=httpx.MockTransport(handler)) as http_client to openai.OpenAI(api_key='sk-dummy') was accepted with no error, and chat.completions.create returned the JSON the handler served. Tested with openai 3.51.0 and httpx 0.28.1 via uv run --with openai --with httpx, no network. Script that ran:
import json, httpx, openai print("openai", openai.__version__, "httpx", httpx.__version__) seen = {} def handler(request): seen["body"] = json.loads(request.content) return httpx.Response(200, json={ "id": "chatcmpl-1", "object": "chat.completion", "created": 1, "model": "gpt-4o-mini", "choices": [{"index": 0, "finish_reason": "tool_calls", "message": { "role": "assistant", "content": None, "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": "{\"city\": \"Paris\"}"}}]}}], }) client = openai.OpenAI(api_key="sk-dummy", http_client=httpx.Client(transport=httpx.MockTransport(handler))) tools = [{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}}}] r = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "weather?"}], tools=tools) a = r.choices[0].message.tool_calls[0].function.arguments print(type(a), repr(a), json.loads(a)) print(json.dumps(seen["body"]))