C.W.K.
Stream
Lesson 03 of 07 · published

Request 직접 만들기 — wire 의 contract

~22 min · request-body, headers, auth

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Request body 를 직접 만들면 wire 가 보여. JSON key (model, input, tools, instructions, max_output_tokens, stream, tool_choice) 의 정확한 spelling. SDK helper 가 이름 바꾼 거 없음. Snuck-in default 없음.

Debugging 의 layer

'SDK 호출은 동작하는데 hand-rolled 호출은 422?' 답은 거의 항상 한 필드 이름 오타 또는 tool 에 type: "function" 빠진 거. 한 번 수동 build 해 보면 SDK 의 magic 이 사라져 — 단순한 contract 가 보여.

Header 도 직접

Authorization: Bearer <key>, Content-Type: application/json, OpenAI-Beta: ... (beta features 시). Org/project header 도 직접 — OpenAI-Organization, OpenAI-Project.

Streaming 시 Accept

Streaming 호출은 Accept: text/event-stream 박아 — 일부 proxy 는 이게 없으면 SSE 를 buffer 해서 streaming 효과를 죽임.

Code

Hand-rolled chat.completions request·python
import os, httpx

url = "https://api.openai.com/v1/chat/completions"
headers = {
    "Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}",
    "Content-Type": "application/json",
}
body = {
    "model": "gpt-4.1",
    "messages": [
        {"role": "developer", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"},
    ],
    "temperature": 0.7,
    "max_completion_tokens": 1024,
}

async with httpx.AsyncClient(timeout=httpx.Timeout(connect=10, read=120, write=10, pool=5)) as client:
    resp = await client.post(url, headers=headers, json=body)
    resp.raise_for_status()
    data = resp.json()
    print(data["choices"][0]["message"]["content"])
Hand-rolled responses request with tools·python
url = "https://api.openai.com/v1/responses"
body = {
    "model": "gpt-4.1",
    "input": "Tell me a three sentence bedtime story about a unicorn.",
}
resp = await client.post(url, headers=headers, json=body)
data = resp.json()
# Access output text from response structure
print(data["output"][0]["content"][0]["text"])

External links

Exercise

Body 직접 build — model gpt-4.1, instructions='You are concise', input='What is 2+2?', max_output_tokens=50. POST /v1/responses 를 httpx 로. response.output_text == '4' verify.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.