Part 4: Question to Enhanced Prompt
Goal
Build and deploy a CDK-backed Lambda that accepts a question, a stock ticker, a filing year, and a period — retrieves the relevant 10-Q filing from SEC EDGAR, extracts clean text using your Part 3 utility, constructs an enhanced prompt with the filing as context, invokes the canonical course model, and returns the answer. This is the end-to-end pipeline: the Lambda receives a structured request and returns a grounded answer that the front-end (Partner Bot Web Page) will consume.
Contract
Your Lambda's input and output must conform to the Lambda Contract.
Request:
{
"question": "What was the company's total revenue this quarter?",
"ticker": "AMZN",
"year": 2024,
"period": "Q3"
}Response:
{
"answer": "Based on the 10-Q filing, total net sales for the quarter were $158.9 billion, an increase of 11% compared to the same quarter in the prior year.",
"meta": {
"model": "the canonical model from reference/models.md",
"input_tokens": 9842,
"output_tokens": 287,
"latency_ms": 3200
}
}The period field accepts Q1, Q2, Q3, Q4, or FY. Use the period to locate the correct filing on SEC EDGAR.
Required Reading
Lambda Contract — the schema your Lambda must conform to
Models — use the canonical model identifier
Part 3: Text Extraction — the utility that converts filing HTML to prompt-ready text
Filter Submissions and Retrieve Doc — filing retrieval via EDGAR
Project: CDK Init — CDK project structure
Constraints
Use Python 3.12 and boto3.
Deploy via CDK. Define the Lambda function, its IAM role (Bedrock invoke permission, network egress for SEC EDGAR), and any supporting resources in your CDK stack.
Use the model identifier from Models. Do not hardcode a model ID.
Validate the incoming event against the contract schema. Return a structured error (not a stack trace) for invalid requests.
Use your Part 3 text-extraction utility to convert the filing HTML to plain text before injecting it into the prompt.
Set a token budget for the filing context that leaves room for the question, the system instructions, and the model's response. A reasonable default: reserve 80% of the model's context window for the filing text.
Include a custom
User-Agentheader on all SEC EDGAR HTTP requests.Do not provide complete solution code. Your implementation is the deliverable.
Acceptance Criteria
Your Lambda accepts a valid contract request and returns a valid contract response.
Given an invalid request (missing field, invalid
periodvalue, non-uppercase ticker), your Lambda returns a structured validation error — not an unhandled exception.The Lambda retrieves the correct 10-Q filing for the specified ticker, year, and period from SEC EDGAR.
The filing HTML is converted to plain text using your Part 3 extraction utility, respecting the token budget.
The enhanced prompt includes both the user's question and the extracted filing text as context.
The model's response answers the question using information from the filing (verifiable by reading the source document).
The
metaobject in the response includes the model identifier, input token count, output token count, and latency.The Lambda is deployed via CDK with appropriate IAM permissions (Bedrock
InvokeModel, network egress).
Hints
The Lambda handler orchestrates four steps in sequence:
Validate — check that all required fields are present and conform to the contract. Return early with an error response if validation fails.
Retrieve — use your EDGAR API library to fetch the filing HTML for the given ticker, year, and period. Map the
periodvalue to the correct fiscal quarter endpoint on EDGAR.Extract — pass the HTML through your Part 3 text-extraction function with a token budget.
Invoke — construct the enhanced prompt and call the model via the Converse API.
For the prompt structure, a pattern that works well:
The instruction to answer "only from the filing" reduces hallucination. Including the ticker, period, and year in the prompt gives the model context about what document it is reading.
For the meta object, the Converse API response includes token usage in the usage field (inputTokens, outputTokens). Capture latency by timing the API call with time.perf_counter().
Map period to the fiscal quarter when searching EDGAR submissions. A company filing a 10-Q for Q2 of 2024 reports the quarter ended June 30, 2024 (for most calendar-year filers). Your EDGAR library's filter_submissions function should accept a form type (10-Q) and date range to locate the correct filing.
For FY (full-year), the relevant form type is 10-K, not 10-Q. Handle this case explicitly in your retrieval logic.
Pick a recent filing and verify the Lambda's answer against the source document. The integration test is: invoke the Lambda, read the answer, open the filing on EDGAR, and confirm the figure appears in the text.
Last verified: 2026-06
Last updated