MCPcopy Create free account
hub / github.com/Roy3838/Observer / _stream_fireworks_response

Method _stream_fireworks_response

api/fireworks_handler.py:181–211  ·  view source on GitHub ↗

Stream SSE chunks from Fireworks API.

(self, headers: dict, payload: dict, display_model_name: str, actual_model_id: str)

Source from the content-addressed store, hash-verified

179 return response_data
180
181 async def _stream_fireworks_response(self, headers: dict, payload: dict, display_model_name: str, actual_model_id: str):
182 """Stream SSE chunks from Fireworks API."""
183 try:
184 client = get_http_client()
185 async with client.stream("POST", self.FIREWORKS_API_URL, headers=headers, json=payload) as response:
186 response.raise_for_status()
187 async for line in response.aiter_lines():
188 if line.startswith("data: "):
189 # Replace actual model ID with display name in streaming chunks
190 chunk_data = line[6:] # Remove "data: " prefix
191 if chunk_data != "[DONE]":
192 try:
193 chunk_json = json.loads(chunk_data)
194 if "model" in chunk_json:
195 chunk_json["model"] = display_model_name
196 yield f"data: {json.dumps(chunk_json)}\n\n"
197 except json.JSONDecodeError:
198 # If we can't parse, just forward as-is
199 yield f"data: {chunk_data}\n\n"
200 else:
201 yield f"data: {chunk_data}\n\n"
202 except httpx.RequestError as exc:
203 logger.error(f"Fireworks streaming API request failed: {exc}")
204 yield f"data: {json.dumps({'error': f'Connection error: {exc}'})}\n\n"
205 except httpx.HTTPStatusError as exc:
206 error_body = exc.response.text
207 logger.error(f"Fireworks streaming API error {exc.response.status_code}: {error_body[:500]}")
208 yield f"data: {json.dumps({'error': f'API error ({exc.response.status_code}): {error_body}'})}\n\n"
209 except Exception as exc:
210 logger.exception(f"Unexpected error in Fireworks streaming for model {actual_model_id}")
211 yield f"data: {json.dumps({'error': f'Unexpected error: {exc}'})}\n\n"

Callers 1

handle_requestMethod · 0.95

Calls 2

get_http_clientFunction · 0.90
errorMethod · 0.80

Tested by

no test coverage detected