ReAct (Reasoning + Acting) + Custom tool for Healthcare NL API + Gemini 2.0 + LangChain

Source notebook

Repo path: gemini/use-cases/healthcare/react_gemini_healthcare_api.ipynb · Open on GitHub · intermediate

Builds a LangChain ReAct agent using Gemini 2.0 and Healthcare NLP to suggest medical billing codes.

Summary

The notebook shows how to authenticate to Google Cloud, enable Vertex AI and Cloud Healthcare API, and call Healthcare NLP on a discharge report. It extracts medical entities and relationships, visualizes them with NetworkX, then wraps the Healthcare NLP call as a LangChain StructuredTool. A Gemini 2.0 Flash ReAct agent uses that tool to generate ICD-10, CPT, DRG, and HCPCS-style coding output, with an optional self-consistency loop over repeated runs.

Key code patterns

Initialize Vertex AI

import vertexai
vertexai.init(project=PROJECT_ID, location=LOCATION)

The LangChain VertexAI wrapper depends on Vertex AI being initialized with the project and region.

Call Healthcare NLP

url = f"https://healthcare.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/services/nlp:analyzeEntities"
headers = {"Authorization": f"Bearer {creds.token}", "X-Goog-User-Project": PROJECT_ID}
data = {"documentContent": text, "licensedVocabularies": ["SNOMEDCT_US", "ICD10CM"]}
response = requests.post(url, headers=headers, data=json.dumps(data))

The notebook sends clinical text to Healthcare NLP and requests SNOMED CT US and ICD-10-CM vocabularies.

Extract medical terms

type_categories = {"PROBLEM", "MEDICINE", "MEDICAL_DEVICE", "PROCEDURE", "LABORATORY_DATA"}
return list({
    entity["text"]["content"]
    for entity in response_json["entityMentions"]
    if entity["type"] in type_categories
})

The custom tool narrows Healthcare NLP entities to terms useful for diagnosis, procedures, medicines, and lab data.

Register custom tool

tools = [StructuredTool.from_function(list_of_medical_terms)]

LangChain exposes the Healthcare NLP helper as an external tool for the ReAct agent.

Create ReAct agent

llm = VertexAI(model_name="gemini-2.0-flash", max_output_tokens=8042, temperature=0)
agent = initialize_agent(
    tools, llm, verbose=True, max_execution_time=1000,
    max_iterations=3, handle_parsing_errors=True,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
)

Gemini is configured through LangChain and paired with a structured zero-shot ReAct agent.

Self-consistency voting

outputs = []
for _ in range(num_of_attempts):
    tmp = agent.run(prompt)
    outputs.append(tmp)
    temp_vote = outputs.count(tmp)
    if temp_vote > vote:
        vote = temp_vote
        final_output = tmp

The notebook repeats the same prompt and keeps the most frequent response as the final output.

Models & APIs used

  • Models: gemini-2.0-flash
  • APIs / services: Vertex AI, Cloud Healthcare API, Healthcare Natural Language API
  • SDKs / libraries: google-cloud-aiplatform, vertexai, langchain, langchain-google-vertexai, google-auth, requests, pandas, networkx, matplotlib

When to use this

Use this pattern to prototype a human-reviewed medical coding assistant that augments Gemini with Healthcare NLP entity extraction.

Gotchas & caveats

  • The notebook requires enabling aiplatform.googleapis.com and healthcare.googleapis.com.
  • Colab requires explicit google.colab authentication, while Vertex AI Workbench does not.
  • The Healthcare NLP REST call requires an OAuth access token from google.auth.default().
  • The code sets LOCATION to us-central1 and configures gcloud healthcare/location to that value.
  • The notebook installs langchain==0.1.16 and tells users to restart the runtime after installation.
  • The notebook explicitly recommends human-in-the-loop review for medical applications.

Best practices

  • Use Healthcare NLP to preprocess unstructured medical records before asking the LLM for code suggestions.
  • Limit extracted tool terms to clinically relevant entity categories before passing them to the agent.
  • Use temperature 0 for more deterministic medical coding output.
  • Hide ReAct reasoning steps with verbose=False when reasoning traces should not be shown.
  • Use self-consistency by running repeated attempts and selecting the majority response.
  • Keep a human in the loop because the notebook is meant to accelerate medical coding, not replace a human coder.