OpenAI Service Client#

Source: examples/clients/openai_service_client.py

Introduction#

For hosted deployments, OpenAI platform docs and the Responses API capture production invocation behavior, while function-calling guidance clarifies structured tool invocation expectations. This example shows the direct OpenAI service client contract with traceable request/response handling.

Technical Implementation#

  1. Configure Tracer with JSONL + console output so each run emits machine-readable traces and lifecycle logs.

  2. Build the runtime surface (public APIs only) and execute OpenAIServiceLLMClient.generate(...) with a fixed request_id.

  3. Construct LLMRequest inputs and call generate through the selected client implementation.

  4. Print a compact JSON payload including trace_info for deterministic tests and docs examples.

        flowchart LR
    A["Input prompt or scenario"] --> B["main(): runtime wiring"]
    B --> C["OpenAIServiceLLMClient.generate(...)"]
    C --> D["LLMRequest/LLMResponse contracts wrap provider behavior"]
    C --> E["Tracer JSONL + console events"]
    D --> F["ExecutionResult/payload"]
    E --> F
    F --> G["Printed JSON output"]
    
 1from __future__ import annotations
 2
 3import json
 4from pathlib import Path
 5
 6import design_research_agents as drag
 7
 8
 9def _build_payload() -> dict[str, object]:
10    assert drag.AzureOpenAIServiceLLMClient.__name__ == "AzureOpenAIServiceLLMClient"
11    # Build the hosted OpenAI client using public runtime APIs, then execute one representative request.
12    with drag.OpenAIServiceLLMClient(
13        name="openai-prod",
14        default_model="gpt-4o-mini",
15        api_key_env="OPENAI_API_KEY",
16        api_key="example-key-for-config-demo",
17        base_url="https://api.openai.com/v1",
18        max_retries=4,
19        model_patterns=("gpt-4o-mini", "gpt-4o-*"),
20    ) as client:
21        description = client.describe()
22        prompt = "In one sentence, when should engineering teams use multi-agent design critique?"
23        response = client.generate(
24            drag.LLMRequest(
25                messages=(
26                    drag.LLMMessage(role="system", content="You are a concise engineering design assistant."),
27                    drag.LLMMessage(role="user", content=prompt),
28                ),
29                model=client.default_model(),
30                temperature=0.0,
31                max_tokens=120,
32            )
33        )
34        llm_call = {
35            "prompt": prompt,
36            "response_text": response.text,
37            "response_model": response.model,
38            "response_provider": response.provider,
39            "response_has_text": bool(response.text.strip()),
40        }
41        response_contract = drag.LLMResponse(
42            text=response.text,
43            model=response.model,
44            provider=response.provider,
45        )
46        return {
47            "client_class": description["client_class"],
48            "default_model": description["default_model"],
49            "llm_call": llm_call,
50            "llm_response_contract_preview": {
51                "model": response_contract.model,
52                "provider": response_contract.provider,
53            },
54            "backend": description["backend"],
55            "capabilities": description["capabilities"],
56            "server": description["server"],
57        }
58
59
60def main() -> None:
61    """Run traced OpenAI service client call payload."""
62    # Fixed request id keeps traces and docs output deterministic across runs.
63    request_id = "example-clients-openai-service-call-001"
64    tracer = drag.Tracer(
65        enabled=True,
66        trace_dir=Path("artifacts/examples/traces"),
67        enable_jsonl=True,
68        enable_console=True,
69    )
70    payload = tracer.run_callable(
71        agent_name="ExamplesOpenAIServiceClientCall",
72        request_id=request_id,
73        input_payload={"scenario": "openai-service-client-call"},
74        function=_build_payload,
75    )
76    assert isinstance(payload, dict)
77    payload["example"] = "clients/openai_service_client.py"
78    payload["trace"] = tracer.trace_info(request_id)
79    # Print the results
80    print(json.dumps(payload, ensure_ascii=True, indent=2, sort_keys=True))
81
82
83if __name__ == "__main__":
84    main()

Expected Results#

Run Command

PYTHONPATH=src python3 examples/clients/openai_service_client.py

Example output captured with DRA_EXAMPLE_LLM_MODE=deterministic (timestamps, durations, and trace filenames vary by run):

{
  "backend": {
    "api_key_env": "OPENAI_API_KEY",
    "base_url": "https://api.openai.com/v1",
    "default_model": "gpt-4o-mini",
    "kind": "openai_service",
    "max_retries": 4,
    "model_patterns": [
      "gpt-4o-mini",
      "gpt-4o-*"
    ],
    "name": "openai-prod"
  },
  "capabilities": {
    "json_mode": "prompt+validate",
    "max_context_tokens": null,
    "streaming": false,
    "tool_calling": "best_effort",
    "vision": false
  },
  "client_class": "OpenAIServiceLLMClient",
  "default_model": "gpt-4o-mini",
  "example": "clients/openai_service_client.py",
  "llm_call": {
    "prompt": "In one sentence, when should engineering teams use multi-agent design critique?",
    "response_has_text": true,
    "response_model": "gpt-4o-mini",
    "response_provider": "example-test-monkeypatch",
    "response_text": "Use multi-agent critique when decisions have high risk and need diverse failure analysis."
  },
  "llm_response_contract_preview": {
    "model": "gpt-4o-mini",
    "provider": "example-test-monkeypatch"
  },
  "server": null,
  "trace": {
    "request_id": "example-clients-openai-service-call-001",
    "trace_dir": "artifacts/examples/traces",
    "trace_path": "artifacts/examples/traces/run_20260222T162206Z_example-clients-openai-service-call-001.jsonl"
  }
}

References#