Hello,
I'm encountering an issue when using TTMSFNCCustomCloudBase
on Windows to call a server-sent events (SSE) endpoint. The endpoint should return data as a real-time stream, but instead the response is fully buffered and only delivered once the entire stream is complete.
Important note: When I run the exact same request using curl --no-buffer
from the Windows command line, the response is streamed correctly in real time — so the issue is not with the Gemini API or Windows itself, but appears specific to how the TMS component handles the response.
This problem happens with both .Curl(...)
and .ExecuteRequest(...)
.
Here’s a minimal FireMonkey example using .Curl(...)
that shows the issue:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types,
FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.TMSFNCCloudBase;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure RequestResultStringEvent(const AResult: string);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.RequestResultStringEvent(const AResult: string);
begin
Memo1.Lines.Add(AResult);
end;
procedure TForm1.Button1Click(Sender: TObject);
var CurlCmd: string;
const YOUR_API_KEY='AIzaSyBNlXAPvYxukCSh0A28oddLadwVB2xczck'; // key from https://aistudio.google.com/apikey
begin
CurlCmd := 'CURL "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key='+YOUR_API_KEY+'" ' +
'-H "Content-Type: application/json" ' +
'--no-buffer ' +
'-d ''{"contents": [{"parts": [{"text": "Explain how AI works"}]}]}''';
Memo1.Lines.Add('Starting: '+CurlCmd);
TTMSFNCCustomCloudBase.Curl(CurlCmd, RequestResultStringEvent);
end;
end.
Even when I build and execute a structured request using ExecuteRequest(...)
, I get the same buffering behavior:
procedure TGeminiClient.BuildRequest(id: string = '');
const
cHeaderContent = 'Content-Type';
cHeaderContentValue = 'application/json';
cHostGemini = 'https://generativelanguage.googleapis.com';
cPathGemini = 'v1beta/models/';
begin
Request.Headers.Clear;
Request.AddHeader(cHeaderContent, cHeaderContentValue);
Request.Method := rmPOST;
Request.Name := id;
Request.Host := cHostGemini;
Request.Path := cPathGemini + FSettings.Model + ':streamGenerateContent?alt=sse&key=' + FSettings.APIKey;
Request.PostData := BuildPostData();
end;
function TGeminiClient.Execute(id: string = ''): boolean;
begin
Inc(FBusyCount);
BuildRequest(id);
ExecuteRequest(DoGeminiRequest, DoStreamProgress, True);
Result := True;
end;
Project1.dpr (215 Bytes)
Project1.dproj (73.7 KB)
Unit1.pas (1.3 KB)
Is there any setting or workaround in the TMS FNC Cloud framework to allow processing the stream in real time without buffering?
Also, perhaps I’ve missed some documentation or manual that explains how TTMSFNCCustomCloudBase
handles streamed responses? If there's something I should review, I’d appreciate a pointer.
P.S. Unrelated, but is it possible to use the TMS FNC REST client to upload a binary file (e.g., as multipart/form-data or raw bytes)? Or is that not its intended use?
Thanks again for your help!
Thanks in advance for your help!