zooski1099
08/14/2025, 4:31 PMaaronv (BAML)
08/14/2025, 4:51 PMzooski1099
08/14/2025, 6:40 PM// GPT-5 Client
client<llm> CustomGPT5 {
provider "openai-responses"
options {
model "gpt-5"
api_key env.OPENAI_API_KEY
}
}
// baml classes/functions
class SourceDocSummary {
summary string
}
function SummarizeSourceDoc(doc: pdf) -> SourceDocSummary {
client CustomGPT5
prompt #"
Summarize the following document in few paragraphs (roughly 3-6). The summary should be as concise as possible while including all pertinent details.
{{ doc }}
{{ ctx.output_format }}
"#
}
test SummarizeSourceDocTest {
functions [SummarizeSourceDoc]
args {
doc {
file "./test_images/sample_doc.pdf"
}
}
}zooski1099
08/14/2025, 6:41 PM// python usage
def stream_sourcedoc_summary(request, sourcedoc_id: int):
"""Stream a generated summary for a SourceDoc using BAML streaming."""
sourcedoc = get_object_or_404(SourceDoc, id=sourcedoc_id)
sd = cast(Any, sourcedoc)
if not sd.url:
return HttpResponseBadRequest("No PDF URL available for this SourceDoc")
def event_stream() -> Iterator[bytes]:
try:
pdf = baml_py.Pdf.from_url(sd.url)
stream = b.stream.SummarizeSourceDoc(pdf)
begin_streaming = False
last_len = 0
for partial in stream:
# partial is stream_types.SourceDocSummary with optional fields
if not begin_streaming and partial.summary and not partial.summary.startswith("[json"):
begin_streaming = True
if begin_streaming and partial.summary:
current_text = partial.summary
if current_text is None:
continue
if last_len < len(current_text):
chunk = current_text[last_len:]
last_len = len(current_text)
if chunk:
yield chunk.encode('utf-8')
final_response = stream.get_final_response()
if final_response.summary and last_len < len(final_response.summary):
yield final_response.summary[last_len:].encode('utf-8')
yield b"\n"
except Exception as e:
yield f"[error] {str(e)}\n".encode('utf-8')
response = StreamingHttpResponse(event_stream(), content_type='text/plain; charset=utf-8')
return _streaming_headers(response)zooski1099
08/14/2025, 6:42 PMzooski1099
08/14/2025, 6:43 PM[error] BamlClientHttpError(client_name=CustomGPT5, message=Request failed with status code: 400 Bad Request,
{
"error": {
"message": "Invalid value: 'file'. Supported values are: 'text', 'refusal', 'image_url', and 'input_audio'.",
"type": "invalid_request_error",
"param": "messages[0].content[1]",
"code": "invalid_value"
}
}, status_code=400)zooski1099
08/14/2025, 6:44 PMaaronv (BAML)
08/14/2025, 6:46 PMzooski1099
08/14/2025, 6:46 PMVaibhav (BAML π)
08/14/2025, 10:57 PMVaibhav (BAML π)
08/14/2025, 10:57 PMVaibhav (BAML π)
08/14/2025, 10:58 PMzooski1099
08/15/2025, 2:39 PMzooski1099
08/15/2025, 2:41 PMfunction SummarizeSourceDoc(doc: pdf) -> SourceDocSummary {
client CustomGPT5
prompt #"
{{ _.role("user") }}
Summarize the following document in few paragraphs (roughly 3-6). The summary should be as concise as possible while including all pertinent details.
{{ doc }}
{{ ctx.output_format }}
"#
}
And the curl from the playground:
curl -X POST 'https://api.openai.com/v1/responses' -H "authorization: Bearer $OPENAI_API_KEY" -H "content-type: application/json" -d "{
\"model\": \"gpt-5\",
\"input\": \"user: Summarize the following document in few paragraphs (roughly 3-6). The summary should be as concise as possible while including all pertinent details. Answer in JSON using this schema:\n{\n summary: string,\n}\"
}"Vaibhav (BAML π)
08/15/2025, 2:46 PMVaibhav (BAML π)
08/15/2025, 2:50 PMVaibhav (BAML π)
08/15/2025, 2:50 PMopenai works, but openai-responses doesn't?zooski1099
08/15/2025, 2:51 PMVaibhav (BAML π)
08/15/2025, 2:52 PMVaibhav (BAML π)
08/15/2025, 2:52 PMVaibhav (BAML π)
08/15/2025, 2:52 PMVaibhav (BAML π)
08/15/2025, 2:52 PMzooski1099
08/15/2025, 2:53 PMopenai is now working and passing in the file correctly. for openai-responses, i'm not getting any api errors but it's not sending the file. and the curl reflects that:
curl -X POST 'https://api.openai.com/v1/responses' -H "authorization: Bearer $OPENAI_API_KEY" -H "content-type: application/json" -d "{
\"model\": \"gpt-5\",
\"input\": \"user: Summarize the following document in few paragraphs (roughly 3-6). The summary should be as concise as possible while including all pertinent details. Answer in JSON using this schema:\n{\n summary: string,\n}\"
}"zooski1099
08/15/2025, 2:54 PMdoc):
function SummarizeSourceDoc(doc: pdf) -> SourceDocSummary {
client CustomGPT5
prompt #"
{{ _.role("user") }}
Summarize the following document in few paragraphs (roughly 3-6). The summary should be as concise as possible while including all pertinent details.
{{ doc }}
{{ ctx.output_format }}
"#
}
test SummarizeSourceDocTest {
functions [SummarizeSourceDoc]
args {
doc {
file "./test_images/McKenzie Handwritten Mortgage.pdf"
}
}
}Vaibhav (BAML π)
08/15/2025, 2:55 PMVaibhav (BAML π)
08/15/2025, 2:55 PMzooski1099
08/15/2025, 2:55 PMzooski1099
08/15/2025, 2:56 PMVaibhav (BAML π)
08/15/2025, 2:56 PMzooski1099
08/15/2025, 2:56 PMzooski1099
08/15/2025, 2:56 PM