|
| 1 | +"""E2E coverage for reasoning_content protocol output.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from types import SimpleNamespace |
| 5 | +from typing import Any, Dict, List |
| 6 | + |
| 7 | +import httpx |
| 8 | +import pytest |
| 9 | + |
| 10 | +from agentrun.server import AgentRequest, AgentRunServer |
| 11 | + |
| 12 | + |
| 13 | +def _parse_sse_events(content: str) -> List[Dict[str, Any]]: |
| 14 | + events = [] |
| 15 | + for line in content.splitlines(): |
| 16 | + if not line.startswith("data: "): |
| 17 | + continue |
| 18 | + payload = line[6:] |
| 19 | + if payload == "[DONE]": |
| 20 | + continue |
| 21 | + events.append(json.loads(payload)) |
| 22 | + return events |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def reasoning_app(): |
| 27 | + async def invoke_agent(request: AgentRequest): |
| 28 | + yield SimpleNamespace( |
| 29 | + content="", |
| 30 | + additional_kwargs={"reasoning_content": "thinking"}, |
| 31 | + ) |
| 32 | + yield SimpleNamespace(content="answer", additional_kwargs={}) |
| 33 | + |
| 34 | + return AgentRunServer(invoke_agent=invoke_agent).as_fastapi_app() |
| 35 | + |
| 36 | + |
| 37 | +async def _post_json(app, path: str, payload: Dict[str, Any]) -> httpx.Response: |
| 38 | + async with httpx.AsyncClient( |
| 39 | + transport=httpx.ASGITransport(app=app), |
| 40 | + base_url="http://test", |
| 41 | + ) as client: |
| 42 | + return await client.post(path, json=payload, timeout=60.0) |
| 43 | + |
| 44 | + |
| 45 | +def _set_thinking(monkeypatch, enabled: bool) -> None: |
| 46 | + monkeypatch.setenv( |
| 47 | + "MODEL_PARAMETER_RULES", |
| 48 | + json.dumps({"thinking": enabled}), |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("thinking_enabled", [True, False]) |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_openai_stream_reasoning_content_gate( |
| 55 | + reasoning_app, |
| 56 | + monkeypatch, |
| 57 | + thinking_enabled: bool, |
| 58 | +): |
| 59 | + _set_thinking(monkeypatch, thinking_enabled) |
| 60 | + |
| 61 | + response = await _post_json( |
| 62 | + reasoning_app, |
| 63 | + "/openai/v1/chat/completions", |
| 64 | + { |
| 65 | + "model": "mock-model", |
| 66 | + "messages": [{"role": "user", "content": "Hi"}], |
| 67 | + "stream": True, |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + assert response.status_code == 200 |
| 72 | + events = _parse_sse_events(response.text) |
| 73 | + deltas = [ |
| 74 | + (event.get("choices") or [{}])[0].get("delta") or {} |
| 75 | + for event in events |
| 76 | + ] |
| 77 | + reasoning = "".join(delta.get("reasoning_content", "") for delta in deltas) |
| 78 | + content = "".join(delta.get("content", "") for delta in deltas) |
| 79 | + |
| 80 | + assert content == "answer" |
| 81 | + assert reasoning == ("thinking" if thinking_enabled else "") |
| 82 | + assert all("additional_kwargs" not in delta for delta in deltas) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("thinking_enabled", [True, False]) |
| 86 | +@pytest.mark.asyncio |
| 87 | +async def test_openai_non_stream_reasoning_content_gate( |
| 88 | + reasoning_app, |
| 89 | + monkeypatch, |
| 90 | + thinking_enabled: bool, |
| 91 | +): |
| 92 | + _set_thinking(monkeypatch, thinking_enabled) |
| 93 | + |
| 94 | + response = await _post_json( |
| 95 | + reasoning_app, |
| 96 | + "/openai/v1/chat/completions", |
| 97 | + { |
| 98 | + "model": "mock-model", |
| 99 | + "messages": [{"role": "user", "content": "Hi"}], |
| 100 | + "stream": False, |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + assert response.status_code == 200 |
| 105 | + message = response.json()["choices"][0]["message"] |
| 106 | + assert message["content"] == "answer" |
| 107 | + if thinking_enabled: |
| 108 | + assert message["reasoning_content"] == "thinking" |
| 109 | + else: |
| 110 | + assert "reasoning_content" not in message |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("thinking_enabled", [True, False]) |
| 114 | +@pytest.mark.asyncio |
| 115 | +async def test_agui_reasoning_events_gate( |
| 116 | + reasoning_app, |
| 117 | + monkeypatch, |
| 118 | + thinking_enabled: bool, |
| 119 | +): |
| 120 | + _set_thinking(monkeypatch, thinking_enabled) |
| 121 | + |
| 122 | + response = await _post_json( |
| 123 | + reasoning_app, |
| 124 | + "/ag-ui/agent", |
| 125 | + {"messages": [{"role": "user", "content": "Hi"}]}, |
| 126 | + ) |
| 127 | + |
| 128 | + assert response.status_code == 200 |
| 129 | + events = _parse_sse_events(response.text) |
| 130 | + event_types = [event["type"] for event in events] |
| 131 | + reasoning = "".join( |
| 132 | + event.get("delta", "") |
| 133 | + for event in events |
| 134 | + if event["type"] == "REASONING_MESSAGE_CONTENT" |
| 135 | + ) |
| 136 | + content = "".join( |
| 137 | + event.get("delta", "") |
| 138 | + for event in events |
| 139 | + if event["type"] == "TEXT_MESSAGE_CONTENT" |
| 140 | + ) |
| 141 | + |
| 142 | + assert content == "answer" |
| 143 | + if thinking_enabled: |
| 144 | + assert reasoning == "thinking" |
| 145 | + assert event_types.index("REASONING_MESSAGE_CONTENT") < event_types.index( |
| 146 | + "TEXT_MESSAGE_START" |
| 147 | + ) |
| 148 | + else: |
| 149 | + assert reasoning == "" |
| 150 | + assert all( |
| 151 | + not event_type.startswith("REASONING") |
| 152 | + for event_type in event_types |
| 153 | + ) |
0 commit comments